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

2024-06-07 18:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

来自bfr.readLine()null

但是,如果我在终端上直接运行python文件,则不会出现问题:

python C:/Machine_Learning/Text_Analysis/Ontology_based.py

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

以下代码的结果是:

运行Python开始:

First Line: null

Picked up _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);}
    }

}

Tags: textnewreadlinestringlineanalysismachineout
3条回答

通常在使用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);}
    }

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

当脚本被终止/终止时,从stdin读取将返回null。做一个等待的过程,看看exitValue是什么。如果不是0,那么很可能您的脚本正在消亡。

我试着用一个只写一个值的哑脚本来实现它。确保从python打印所有错误信息。

相关问题 更多 >

    热门问题