使用Popen执行Java JAR:无法查找或加载主类JAR

时间:2017-07-31 21:40:49

标签: python subprocess popen

我的Java JAR在linux命令行中运行良好,我希望通过python脚本执行它。

尝试Popen时出现以下错误:

Error: Could not find or load main class jar

有什么想法吗?

到目前为止我尝试过的事情:

  1. JAR文件的命令行执行。检查。
  2. Popen包含简单的java-version。检查。
  3. 更新cwd。检查。
  4. 使用Java调用:

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

    命令失败:

    >>> 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
    

    我错过的任何指示?

1 个答案:

答案 0 :(得分:1)

在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

更改为:javaCall = subprocess.Popen(['java', '-jar' , ~~~