我有一个简单的问题:我试图从java应用程序C调用以下命令行:/ phantomjs / phantomjs chart / chart.js
我尝试过:
public static void go3(){
Runtime rt=Runtime.getRuntime();
try{
final Process pr=rt.exec("cmd C:/phantomjs/phantomjs chart/chart.js");
final int exitCode=pr.waitFor();
if(exitCode!=0){ throw new RuntimeException("program didnt exit with 0, but with "+exitCode); }
// System.out.println(pr.toString());
// int exitStatus=pr.waitFor();
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch(InterruptedException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done");
}
但我得到退出代码-1。我查看了有关stackoverflow的各种教程/问题,但它们都运行了一些简单的例子,我很难理解如何在.exec("what goes here?")
内编写部分
答案 0 :(得分:2)
找到答案:
public static void go4(){
String[] command={"cmd","/k","cd /phantomjs&&phantomjs chart/chart.js"};
Process p;
try{
p=Runtime.getRuntime().exec(command);
PrintWriter stdin=new PrintWriter(p.getOutputStream());
stdin.close();
int returnCode;
returnCode=p.waitFor();
System.out.println("Return code = "+returnCode);
}catch(IOException e1){
e1.printStackTrace();
}catch(InterruptedException e){
e.printStackTrace();
}
}