0

I am invoking a java command in python with this code [1], but I get the error in [2]. I am using python (not jpython) and I would like that python could understand that this is an exception, so that it can throw it. Is there a way to catch java exception in python?

[1] code to call java command command = "hadoop jar examples.jar wordcount /input1 /output1"

process = subprocess.Popen(
    shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()
output = out[stdout]

[2] java exception

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
    at org.apache.hadoop.mapred.examples.MyWordCount.main(MyWordCount.java:146)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.hadoop.util.ProgramDriver$ProgramDescription.invoke(ProgramDriver.java:71)
    at org.apache.hadoop.util.ProgramDriver.run(ProgramDriver.java:144)
    at org.apache.hadoop.mapred.examples.ExampleDriver.main(ExampleDriver.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
xeon123
  • 819
  • 1
  • 10
  • 25
  • possible duplicate of [How to catch exception output from Python subprocess.check\_output()?](http://stackoverflow.com/questions/24849998/how-to-catch-exception-output-from-python-subprocess-check-output) –  Sep 21 '15 at 15:35
  • what is `output = out[stdout]`? – Padraic Cunningham Sep 21 '15 at 15:38
  • 1
    Aren't exceptions on stderr? You will probably have to parse the java exception. – AChampion Sep 21 '15 at 15:38
  • I've assumed (risky!!!) stdout is defined as `0` given `communicate()` returns a tuple of `(stdoutdata, stderrdata)`. – AChampion Sep 21 '15 at 15:42
  • You are going to have to catch the java error in the java code and pass the result back to the python program. As as been mentioned above communicate() returns a tuple (stdoutdata, stderrdata), so `out, err = process.communicate()` in the python code. That is unless I have the wrong end of the stick completely. – Rolf of Saxony Sep 21 '15 at 15:54
  • @Padraic Cunningham Now I got the error, because of `(stdoutdata, stderrdata)`. – xeon123 Sep 21 '15 at 16:05
  • @user3240152, so `output = out[stdout]` stderr or stdout? What does process.returncode output? – Padraic Cunningham Sep 21 '15 at 16:06
  • if you have a control over the java implementation, make sure to return an exit code which the python implementation will detect. if you do not want to exit the java program when such an exception is thrown, then you will have to parse the java program output and recognize the exception, or overload an environment variable (bad practice), and sample it on the python implementation. – Mr. Sep 21 '15 at 16:07
  • 1
    @PadraicCunningham yes you right. The var should have out[stderr]. Now I got the error. – xeon123 Sep 21 '15 at 16:09

0 Answers0