Java Processbuilder Not Able To Run Python Script In Java
Solution 1:
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.*;
publicclassSimilarity {
/**
*
* @param args
*
*/publicstaticvoidmain(String[] args){
try{
StringpythonPath="C:/Machine_Learning/Text_Analysis/Ontology_based.py";
//String pythonExe = "C:/Users/AppData/Local/Continuum/Anaconda/python.exe";ProcessBuilderpb=newProcessBuilder(Arrays.asList("<Absolute Path to Python>/python", pythonPath));
Processp= pb.start();
BufferedReaderbfr=newBufferedReader(newInputStreamReader(p.getInputStream()));
Stringline="";
System.out.println("Running Python starts: " + line);
intexitCode= 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);}
}
}
Solution 2:
Reading from stdin returns null when the script is killed/dies. Do a Process#waitFor and see what the exitValue
is. If it isn't 0 then it's highly probable that your script is dying.
I'd try making it work with a dumb script that only writes a value. Make sure that you print all error information from python.
Solution 3:
try {
Processp= Runtime.getRuntime().exec(
"python D://input.py ");
BufferedReaderin=newBufferedReader(newInputStreamReader(
p.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
p.waitFor();
} catch (Exception e) {
}
Solution 4:
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);
}
Solution 5:
I tried this one. This script runs a python file with the argument in Java. It also logs about which line, your program is executing. Hope this Helps.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
publicclassTest {
publicstaticvoidmain(String... args)throws IOException {
ProcessBuilderpb=newProcessBuilder("python","samples/test/table_cv.py","1.pdf");
pb.redirectErrorStream(true);
Processproc= pb.start();
Readerreader=newInputStreamReader(proc.getInputStream());
BufferedReaderbf=newBufferedReader(reader);
String s;
while ((s = bf.readLine()) != null) {
System.out.println(s);
}
}
}
Post a Comment for "Java Processbuilder Not Able To Run Python Script In Java"