0

I use javaw.exe in a Windows command prompt and it returns immediately after spawning my Swing java program.

But if I use Python's subprocess.call() to do the same thing, it hangs.

import subprocess
retval = subprocess.call(['javaw.exe','-jar','myjar.jar',arg1,arg2])

What am I doing wrong and why is there this difference?

1 Answer 1

2

subprocess.call will wait for the process (javaw) to complete, as it says in the docs:

Run the command described by args. Wait for command to complete, then return the returncode attribute.

You should probably use subprocess.Popen instead.

Check out the docs for replacing the os.spawn family:

pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid

In your case, this is probably

pid = subprocess.Popen(["javaw.exe", "-jar", "myjar.jar", arg1, arg2])

perhaps adjusted to get the absolute path to javaw.exe, or shell=True, depending on your mood and needs.

Sign up to request clarification or add additional context in comments.

2 Comments

But javaw.exe completes immediately when I run it from a command prompt... or does it? How does it work in the command prompt?
It depends on what your jar does. The process may continue running in the background, but Windows is just giving control back to the shell. Have you used Windows Task Manager to look for the running process after you launch from the shell?

Your Answer

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.