0

这是我的代码

public void addImg(){
    try{
        //Attempt 1
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("/usr/bin/python2.7 ../wc.py");
        p.waitFor();
        p.destroy();

        //Attempt 2
        p = r.exec("python2.7 ../wc.py");
        p.waitFor();
        p.destroy();
    }catch (Exception e){
        String cause = e.getMessage();
        System.out.print(cause);
    }
}

我一直试图让它工作大约一个小时,没有,似乎没有任何工作,并且没有显示错误。我更关心如何调试它,但是我的代码有什么问题可以表明为什么这个脚本没有执行吗?

4

3 回答 3

2

如果 exec() 方法没有立即抛出异常,则仅意味着它可以执行外部进程。然而,这并不意味着它成功执行或它甚至正确执行。

有很多方法可以检查外部进程是否成功执行,下面列出了少数几种方法:

使用 Process.getErrorStream() 和 Process.getInputStream() 方法从外部进程读取输出。

查看外部进程的退出代码,代码0代表正常执行,否则可能发生错误。

考虑添加以下代码以进行调试:

public void addImg(){
    try{
        Runtime r = Runtime.getRuntime();

        //Don't use this one...
        //Process p = r.exec("/usr/bin/python2.7 ../wc.py");
        //p.waitFor();
        //p.destroy();

        //Use absolute paths (e.g blahblah/foo/bar/wc.py)
        p = r.exec("python2.7 ../wc.py");

        //Set up two threads to read on the output of the external process.
        Thread stdout = new Thread(new StreamReader(p.getInputStream()));
        Thread stderr = new Thread(new StreamReader(p.getErrorStream()));

        stdout.start();
        stderr.start();

        int exitval = p.waitFor();
        p.destroy();

        //Prints exit code to screen.
        System.out.println("Process ended with exit code:" + exitval);
    }catch(Exception e){
        String cause = e.getMessage();
        System.out.print(cause);
    }
}

private class StreamReader implements Runnable{
    private InputStream stream;
    private boolean run;

    public StreamReader(Inputstream i){
        stream = i;
        run = true;
    }

    public void run(){
        BufferedReader reader;
        try{
            reader = new BufferedReader(new InputStreamReader(stream));

            String line;

            while(run && line != null){
                System.out.println(line);
            }
        }catch(IOException ex){
            //Handle if you want...
        }finally{
            try{
                reader.close();
            }catch(Exception e){}
        }
    }
}

此外,尝试在调用外部应用程序时使用 ProcessBuilder,尽管需要更多代码,但我发现它更容易使用。

于 2013-10-14T17:20:07.097 回答
0

You need to look at the result returned by running the command instead of just catching exceptions.

Take a look at the exitValue() and methods to get the output and error streams on your Process object.

My guess is that python isn't able to find your program because ../ is resolved by your shell and programs launched using exec aren't run from a shell.

于 2013-10-14T17:15:15.450 回答
0

打印出错误流:

Runtime r = Runtime.getRuntime();
String line;
Process p = r.exec("/usr/bin/python2.7 ../wc.py");
InputStream stdin = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
p.waitFor();
while ( (line = br.readLine()) != null)
    System.out.println("-"+line);
p.destroy();

可能 wc.py 没有找到。

于 2013-10-14T17:20:51.077 回答