0

In a python2.7 script, I'm using subprocess.call to run a java program like this:

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

Most of the time, this works fine, but in some cases the java program errs:

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

The problem is that my python script is then stalled on the subprocess.call() line and can't do the "other stuff".

Is there a way I can edit either the java_command I'm using or the way I'm using subprocess to continue the python script even when the java program hangs?

Note that I can't modify the java program's code.

1 Answer 1

1

I think you want the check_call method from that same package:

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
Sign up to request clarification or add additional context in comments.

3 Comments

Just tried this, to no avail. I think the java program is not fully exiting somehow after its error. When I run the same java command outside the script, from the command line, it gives the same answer but still doesn't return the command-line prompt until I Ctrl-C.
Seems like, based on this, that the exception probably doesn't terminate the JVM, which means that I may be stuck here.
I see what you mean. You're right: the Java program needs a particular flow handling to terminate on the exception. You may want to spawn the process and continue, checking its status occasionally?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.