I am trying to execute python code with java. My code:
public class PyTest {
public static void main(String[] args) {
Runtime.getRuntime().exec("python src\\gui.py");
}
}
Stack trace: Exception in thread "main" java.io.IOException: Cannot run program "src\gui.py": CreateProcess error=193, %1 is not a valid Win32 application
I know, that this error is present while executing python code with 32-bit version interpreter on 64-bit machine. However on my 64-bit machine are installed only 64-bit versions of python.
1 Answer 1
It is the command shell that knows how to run files by file extension, so you need to invoke the cmd.exe Windows shell program:
Runtime.getRuntime().exec("cmd.exe /c python src\\gui.py");
As the javadoc of exec says, the preferred way to run commands is ProcessBuilder, so your code should be:
new ProcessBuilder("cmd.exe", "/c", "python", "src\\gui.py").start();
You can even change the working directory with that:
new ProcessBuilder("cmd.exe", "/c", "python", "gui.py")
.directory(new File("src"))
.start();