Search code examples
pythonsubprocesspopen

Executing Java JAR With Popen: Couldn't Find or Load Main Class JAR


My Java JAR executes fine in linux command line, and I would like to have it executed through a python script.

I get the following error when trying Popen:

Error: Could not find or load main class jar

Any ideas?

What I've tried thus far:

  1. Command line execution of JAR file. Checked.
  2. Popen with simple java and -version. checked.
  3. Update cwd. Checked.

Working Java call:

>>> javaCall = subprocess.Popen(['java', '-version'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Failed command:

>>> javaCall = subprocess.Popen(['java', 'jar' , 
'abs/path/to/jar/abc.jar', 
'arg 1', 'arg 2', 'arg 3'], cwd = 
'/abs/path/where/jar and python files live', stdin=subprocess.PIPE, 
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> output, err = javaCall.communicate()
>>> print err
Error: Could not find or load main class jar

Any pointers I'm missing?


Solution

  • Missing a - in front of jar

    javaCall = subprocess.Popen(['java', 'jar' , 
    'abs/path/to/jar/abc.jar', 
    'arg 1', 'arg 2', 'arg 3'], cwd = 
    '/abs/path/where/jar and python files live', stdin=subprocess.PIPE, 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    >>> output, err = javaCall.communicate()
    >>> print err
    Error: Could not find or load main class jar
    

    Change to: javaCall = subprocess.Popen(['java', '-jar' , ~~~