I use the following lines of code to run a python script:
Runtime rt = Runtime.getRuntime();
String command = PYTHON + " " + "foo.py";
try {
System.out.println("Python Command: " + command);
rt.exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}
Previously it was enough to set PYTHON ="python";
however after setting up Anaconda3 again, this did no longer work, though entering above command via command line did work.
Java threw the following error code: java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified
However I found out, that Java was completely happy, if I gave it the full path to the python.exe inside Anaconda3 and everything worked magically.
This got me curious now, how Runetime.exec
really works, when and how environment variables are used/accepted properly as I find the just described behaviour rather strange.
As always any constructive comment/answer makes me happy.
There are also other users on stackoverflow, who had the same problem, however no conclusive answers were provided. I refer to.