Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么这个python脚本不是';t在java中执行_Java_Python - Fatal编程技术网

为什么这个python脚本不是';t在java中执行

为什么这个python脚本不是';t在java中执行,java,python,Java,Python,这是我的密码 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.

这是我的密码

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

我已经试着让它工作了大约一个小时没有,似乎什么都没有工作,没有显示错误。我更关心的是如何调试这个脚本,但是我的代码是否有任何错误,表明为什么这个脚本没有执行

您需要查看通过运行命令返回的结果,而不仅仅是捕获异常

查看
exitValue()
和获取对象上的输出和错误流的方法

我的猜测是python无法找到您的程序,因为../由您的shell解析,并且使用exec启动的程序不是从shell运行的。

如果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,我发现它更易于使用,尽管需要更多的代码。

打印错误流:

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。

我首先要尝试的是.py文件的绝对路径。退出状态返回1我的退出状态为1,有人提到这可能是因为../在exec()中没有解析,我怎么能重新喜欢它?@JakeSchievink使用脚本的完整路径…/是与当前工作目录相关的相对路径。避免这样做,并说明完整路径(从根开始)。我对linux没有太多经验,但在windows上,它通常以驱动器号(如C)开头:绝对路径的一个示例是C:\scripts\wc.py谢谢您的帮助,现在当我运行它时,它永远不会通过waitFor()method@JakeSchievink它真的在做它的工作吗?未通过waitFor()方法意味着外部应用程序未关闭,因为waitFor()方法在应用程序终止时阻塞。外部应用程序的输出显示了什么?在我的python脚本末尾添加了一个exit()命令,效果很好,再次感谢,祝您愉快