1

我有一个简单的问题:我正在尝试从 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?")

4

1 回答 1

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();
    }

}
于 2013-08-15T11:48:48.327 回答