当Python脚本使用子进程运行java程序时,需要在java错误之后继续前进

时间:2016-11-10 18:37:19

标签: java python subprocess

在python2.7脚本中,我使用subprocess.call来运行这样的java程序:

java_command = "java -jar /path/to/java_program.jar %s %s >> %s" % (infile, outfile, logfile)
subprocess.call(java_command, shell=True)
...
#Do other stuff unrelated to this output

大多数情况下,这种方法很好,但在某些情况下,java程序错误:

Exception in thread "main" java.lang.NullPointerException
    at MyProgram.MainWindow.setProcessing(MainWindow.java:288)

问题是我的python脚本然后在subprocess.call()行停滞,并且不能执行"其他的东西"。

有没有办法可以编辑java_command我使用的方式或者我使用subprocess继续python脚本的方式,即使在java程序中也是如此挂起吗

请注意,我无法修改java程序的代码。

1 个答案:

答案 0 :(得分:1)

我认为你想要同一个包中的check_call方法:

try:
    status = subprocess.check_call(java_command, shell=True)
except CalledProcessError as e:
    # The exception object contains the return code and
    # other failure information.
    ... react to the failure and recover