简体   繁体   English

使用python子进程运行javaw.exe

[英]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. 我在Windows命令提示符中使用javaw.exe,并且在生成我的Swing Java程序后立即返回。

But if I use Python's subprocess.call() to do the same thing, it hangs. 但是,如果我使用Python的subprocess.call()做同样的事情,它将挂起。

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: subprocess.call将等待进程(javaw)完成,如文档中所述:

Run the command described by args. 运行args描述的命令。 Wait for command to complete, then return the returncode attribute. 等待命令完成,然后返回returncode属性。

You should probably use subprocess.Popen instead. 您可能应该使用subprocess.Popen代替。

Check out the docs for replacing the os.spawn family : 查看用于替换os.spawn系列的文档:

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. 可能根据您的心情和需求进行了调整,以获取javaw.exe的绝对路径或shell=True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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