Just give them as separate strings in the array, instead of combining the last two into "val_31 val_32":
String[] command = {"script.py", "run",
"-arg1", "val1",
"-arg2", "val2" ,
"-arg3" , "val_31", "val_32",
};
Otherwise it will escape the space in between val_31 and val_32 because you are telling it that they're a single parameter. Incidentally, you can also use the varargs constructor and skip having to create an array, if you want:
ProcessBuilder probuilder = new ProcessBuilder( "script.py", "run",
"-arg1", "val1",
"-arg2", "val2" ,
"-arg3" , "val_31", "val_32");
Answer from David Robinson on Stack OverflowRunning python from Java using Process Builder - Stack Overflow
python - Building a process with ProcessBuilder from a .py script? - Stack Overflow
Running a python code using process builder Java - Stack Overflow
Java/python using processBuilder - Stack Overflow
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);}
}
}
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.
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.