简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM