1

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.

asked May 14, 2019 at 16:17
0

1 Answer 1

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();
answered May 14, 2019 at 16:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.