I am trying to invoke a python script (part of a larger python project) from a java app. I've tested and tested this, it works exactly as expected from the command line. I've even resorted to making my own stand alone python script to write out a file. It looks like this:
fo = open("bar.txt", "wb")
fo.write("this is a test")
fo.close()
It creates the file bar.txt when run from command line.
I am trying to invoke it from my java app like this:
String pythonScriptPath = "/absolute/path/to/foo.py";
String[] cmd = new String[2];
cmd[0] = "python";
cmd[1] = pythonScriptPath;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
No file is written out. What am I missing?
Runtime.exec(), use aProcessBuilder. It allows, from other things, to set the current working directory of a process before starting it, to redirect std{in,out,err} etc.ProcessBuilderand monitor the error output stream to see if the Python program terminates correctly