[英]Java run phantomjs from command line
我有一個快速的問題:我試圖從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?")
編寫該部分.exec("what goes here?")
找到了答案:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.