我试图让我的java程序使用
执行python脚本import java.lang.Runtime;
public class test
{
public static void main(String[] args)
{
try
{
System.out.println("testing");
Runtime.getRuntime().exec("/usr/bin/python print.py");
}catch(Exception e){System.out.println("not working");}
}
}
然而,终端上没有显示任何内容,(print.py简单打印"这是工作")。与此相反,当我使用
时Runtime.getRuntime().exec("touch dog.txt");
创建名为dog.txt的文件。
我也尝试过
Runtime.getRuntime()exec("./shellscript.sh");
这只是一个运行touch命令的脚本,但也没有用。
不确定这里的问题是什么,更有意思的是,昨天java程序按预期工作,中间时间我的计算机没有大的变化。关于最新情况,有人有任何想法吗?
我没有收到任何错误。
答案 0 :(得分:0)
我猜你必须等到脚本执行结束。
try {
Process process = Runtime.getRuntime().exec("/usr/bin/python print.py");
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
您可以尝试从进程中读取exitCode; - )
答案 1 :(得分:0)
好的,我最终通过使用
取得了成功process p = Runtime.getRuntime().exec("./pyshellthing.sh");
p.waitFor();
shell脚本只执行python脚本。