簡體   English   中英

Java ProcessBuilder 無法在 Java 中運行 Python 腳本

[英]Java ProcessBuilder not able to run Python script in Java

來自bfr.readLine() null

但是,如果我通過觸發直接在終端上運行 python 文件,則沒有問題:

python C:/Machine_Learning/Text_Analysis/Ontology_based.py

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

以下代碼的結果是:

運行 Python 開始:

第一行:空

拿起_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);}
    }

}

通常在使用ProcessBuilder執行命令時,不會考慮PATH變量。 你的python C:/Machine_Learning/Text_Analysis/Ontology_based.py直接在你的 CMD shell 中工作,因為它可以使用PATH變量定位python可執行文件。 請在您的 Java 代碼中提供python命令的絕對路徑。 在下面的代碼中,將<Absolute Path to Python>替換為python命令及其庫的路徑。 通常它會在 Windows 中默認類似於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);}
    }

}

當腳本被殺死/死亡時,從標准輸入讀取返回空值。 執行 Process#waitFor 並查看exitValue是什么。 如果它不是 0,那么您的腳本很可能正在死亡。

我會嘗試使用一個只寫一個值的愚蠢腳本來讓它工作。 確保從 python 打印所有錯誤信息。

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

我試過這個 此腳本使用 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);
        }
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM