我在 Windows 命令提示符中使用 javaw.exe,它在生成我的 Swing java 程序后立即返回。
但是如果我使用Python的subprocess.call()
做同样的事情,它会挂起。
import subprocess
retval = subprocess.call(['javaw.exe','-jar','myjar.jar',arg1,arg2])
我做错了什么以及为什么会有这种差异?
最佳答案
subprocess.call
将等待进程 (javaw) 完成,如文档中所述:
Run the command described by args. Wait for command to complete, then return the returncode attribute.
您可能应该使用subprocess.Popen相反。
查看 replacing the os.spawn family 的文档:
pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid
就您而言,这可能是
pid = subprocess.Popen(["javaw.exe", "-jar", "myjar.jar", arg1, arg2])
可能会根据您的心情和需求进行调整以获取 javaw.exe
或 shell=True
的绝对路径。
关于java - 使用python子进程运行javaw.exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25373224/