Java ProcessBuilder not able to run Python script in Java

13,107

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

}
Share:
13,107
Yue Harriet Huang
Author by

Yue Harriet Huang

Updated on June 15, 2022

Comments

  • Yue Harriet Huang
    Yue Harriet Huang almost 2 years

    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);}
        }
    
    }
    
  • croxy
    croxy over 7 years
    A little explanation of what your code does wouldn't be bad.
  • Raveen Athapaththu
    Raveen Athapaththu about 6 years
    This helped. Thank you.