Jason S
Jason S

Reputation: 189626

using python subprocess to run javaw.exe

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?

Upvotes: 0

Views: 551

Answers (1)

Blair Conrad
Blair Conrad

Reputation: 241744

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.

Upvotes: 2

Related Questions