Java ProcessBuilder无法在Java中运行Python脚本 [英] Java ProcessBuilder not able to run Python script in Java

查看:136
本文介绍了Java ProcessBuilder无法在Java中运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 bfr.readLine()

null

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

python C:/Machine_Learning/Text_Analysis/Ontology_based.py

我的Python脚本的最后一行是>>打印(数据)

以下代码的结果是:

运行Python开始:

第一行:空

提起_JAVA_OPTIONS:-Xmx512M


 包text_clustering;导入java.io. *;公共类相似度{/**** @参数args**/公共静态void main(String [] args){尝试{字符串pythonPath ="C:/Machine_Learning/Text_Analysis/Ontology_based.py";//String pythonExe ="C:/Users/AppData/Local/Continuum/Anaconda/python.exe";ProcessBuilder pb =新的ProcessBuilder("python",pythonPath);进程p = pb.start();BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));字符串行=";System.out.println(正在运行的Python开始:" +行);line = bfr.readLine();System.out.println(第一行:" +行);while((line = bfr.readLine())!= null){System.out.println("Python输出:" +行);}} catch(Exception e){System.out.println(e);}}} 

解决方案

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

 包text_clustering;导入java.io. *;公共类相似度{/**** @参数args**/公共静态void main(String [] args){尝试{字符串pythonPath ="C:/Machine_Learning/Text_Analysis/Ontology_based.py";//String pythonExe ="C:/Users/AppData/Local/Continuum/Anaconda/python.exe";ProcessBuilder pb =新的ProcessBuilder(Arrays.asList(< Python的绝对路径>/python",pythonPath));进程p = pb.start();BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));字符串行=";System.out.println(正在运行的Python开始于:" +行);int exitCode = p.waitFor();System.out.println(退出代码:" + exitCode);line = bfr.readLine();System.out.println(第一行:" +行);while((line = bfr.readLine())!= null){System.out.println("Python输出:" +行);}} catch(Exception e){System.out.println(e);}}} 

null from bfr.readLine()

However, there is no problem if I run the python file directly on terminal by firing:

python C:/Machine_Learning/Text_Analysis/Ontology_based.py

The last line in my Python script is >> print(data)

The result of the following code is:

Running Python starts:

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

}

解决方案

Usually when executing commands using ProcessBuilder, PATH variable is not taken into consideration. Your python C:/Machine_Learning/Text_Analysis/Ontology_based.py is directly working in your CMD shell because it can locate the python executable using the PATH variable. Please provide the absolute path to python command in your Java code. In below code replace <Absolute Path to Python> with the path to python command and its libraries. Usually it will something like C:\Python27\python in Windows by default

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

}

这篇关于Java ProcessBuilder无法在Java中运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆