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);}
}
}
Answer from shazin on Stack OverflowUsually 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);}
}
}
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.
Running python from Java using Process Builder - Stack Overflow
Running a python code using process builder Java - Stack Overflow
java - Using ProcessBuilder to execute a python script with command line options - Stack Overflow
calling python method through java ProcessBuilder() - Stack Overflow
Videos
I don't think reading the executable (a series of binary numbers) will do any good. Also, The class java.lang.ProcessBuilder, in Java 1.5, is used to create operating system processe. Instead you should use the jython API: http://www.jython.org/javadoc/org/python/util/PythonInterpreter.html
Particularly: PyInstance and PythonInterpreter;
Actually, you can't call a Python function from a ProcessBuilder, but you can create a Python program which calls a function from your Python module. And than you can run it from a ProcessBuilder as a command:
python -c 'from pickel4output import pic; pic()'
However, take into attention that since your pic function expects some parameters, you have to create a python code which reads these parameters from a file or a command line and then run the function.
Your script runs, but it does not reach "Step 2", so
gUpdate.parse("BBCOntology.rdf" )
will be the source of the problem. Possibly it is because the file BBCOntology.rdf is not in the current working directory of the Python process. Or it could be that the Python process does not have permission to open that file.
It might be worth reading the error stream from the Python process and printing that out in Java. Use p.getErrorStream() in the same manner that you use p.getInputStream().
Or, easier, add an exception handler to your Python code that catches and prints exception messages to standard out:
import traceback
try:
gUpdate = Graph()
print ".> Step....1"
gUpdate.parse("BBCOntology.rdf" )
print ".> Step....2"
print gUpdate.serialize(format='xml')
print ".> Finished......."
except Exception as exc:
traceback.print_exc()
raise exc
Your Java process should then print the message, which might be informative.
gUpdate = Graph()
print ".> Step....1"
gUpdate.parse("D:\\Desktop\\searchTestJava\\BBCOntology.rdf" )
print ".> Step....2"
The BBCOntology.rdf is in the current working directory of the Python process. So the program can work in python even if I wrote as (gUpdate.parse("BBCOntology.rdf" )).
However, java does not know the directory BBCOntology.rdf is same as the TESTopenBaseOnt.py. Once I add the gUpdate.parse("D:\Desktop\searchTestJava\BBCOntology.rdf" ) , Java can work.
I'm a python dev, like I know literally no Java. But because I'm experienced in python I can at least kinda understand what I'm doing in Java. But I'm stuck on this. I know it's basic shit, I can do this in python, but I just can't read the language so I can't read any help or guides either.
All I need to do really is start a python program from Java as a subprocess, receive it's output stream and be able to send to its input stream.
The python program is a discord bot using async/await stuff and will run continuously so it needs to be uninterrupted, like if it was run from the terminal EXCEPT the Java program can send input and receive output.
I also suck att processes and stuff (this is on Windows btw) and as I said I suck at Java too so sorry if I gave to little information or something. Just tell me if that's the case.
Thanks in advance :D