Java ProcessBuilder无法在Java中运行Python脚本

时间:2014-10-03 01:00:25

标签: java python processbuilder

来自null

bfr.readLine()

但是,如果我通过触发直接在终端上运行python文件没有问题:

  

python C:/Machine_Learning/Text_Analysis/Ontology_based.py

我的Python脚本中的最后一行是>> print(data)

以下代码的结果是:

运行Python启动:

  

第一行:null

     

拿起_JAVA_OPTIONS:-Xmx512M


package text_clustering;

import java.io.*;

public class Similarity {

    /**
     * 
     * @param args
     * 
     */
    public static void main(String[] args){
        try{
            String pythonPath = "C:/Machine_Learning/Text_Analysis/Ontology_based.py";
            //String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";
            ProcessBuilder pb = new ProcessBuilder("python", pythonPath);
            Process p = pb.start();

            BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            System.out.println("Running Python starts: " + line);
            line = bfr.readLine();
            System.out.println("First Line: " + line);
            while ((line = bfr.readLine()) != null){
                System.out.println("Python Output: " + line);


            }

        }catch(Exception e){System.out.println(e);}
    }

}

5 个答案:

答案 0 :(得分:2)

通常在使用 ProcessBuilder 执行命令时,不会考虑PATH变量。您的python C:/Machine_Learning/Text_Analysis/Ontology_based.py直接在您的CMD shell中工作,因为它可以使用python变量找到PATH可执行文件。请在Java代码中提供python命令的绝对路径。在下面的代码中,将<Absolute Path to Python>替换为python命令及其库的路径。通常情况下,默认情况下它会像C:\Python27\python一样

package text_clustering;

import java.io.*;

public class Similarity {

    /**
     * 
     * @param args
     * 
     */
    public static void main(String[] args){
        try{
            String pythonPath = "C:/Machine_Learning/Text_Analysis/Ontology_based.py";
            //String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";
            ProcessBuilder pb = new ProcessBuilder(Arrays.asList("<Absolute Path to Python>/python", pythonPath));
            Process p = pb.start();

            BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            System.out.println("Running Python starts: " + line);
            int exitCode = p.waitFor();
            System.out.println("Exit Code : "+exitCode);
            line = bfr.readLine();
            System.out.println("First Line: " + line);
            while ((line = bfr.readLine()) != null){
                System.out.println("Python Output: " + line);


            }

        }catch(Exception e){System.out.println(e);}
    }

}

答案 1 :(得分:0)

当脚本被终止/死亡时,从stdin读取返回null。做一个进程#waitFor并看看exitValue是什么。如果它不是0那么你的脚本很可能会死亡。

我尝试使用只写一个值的哑脚本。确保从python中打印所有错误信息。

答案 2 :(得分:0)

try {

    Process p = Runtime.getRuntime().exec(
            "python   D://input.py   ");
    BufferedReader in = new BufferedReader(new InputStreamReader(
            p.getInputStream()));

    String line;  
        while ((line = in.readLine()) != null) {  
            System.out.println(line);  
        }  
        in.close();
        p.waitFor();



} catch (Exception e) {
}

答案 3 :(得分:0)

    {
    ProcessBuilder pb = new ProcessBuilder("C:/Python27/python","D://searchTestJava//input.py");
    Process p = pb.start();
    BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));

    System.out.println(".........start   process.........");  
    String line = "";     
    while ((line = bfr.readLine()) != null){
        System.out.println("Python Output: " + line);
    }

    System.out.println("........end   process.......");

    }catch(Exception e){System.out.println(e);}

答案 4 :(得分:0)

我尝试了this个。该脚本运行带有Java参数的python文件。它还会记录您的程序正在执行哪一行。希望对您有所帮助。

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;

    public class Test {
      public static void main(String... args) throws IOException {

        ProcessBuilder pb =
                new ProcessBuilder("python","samples/test/table_cv.py","1.pdf");

        pb.redirectErrorStream(true);
        Process proc = pb.start();

        Reader reader = new InputStreamReader(proc.getInputStream());
        BufferedReader bf = new BufferedReader(reader);
        String s;
        while ((s = bf.readLine()) != null) {
            System.out.println(s);
        }
    }
  }
相关问题