1

所以我一直在使用 subprocess.call从 Python运行一个jar文件,如下所示:

subprocess.call(['java','-jar','jarFile.jar',-a','input_file','output_file'])

它将结果写入外部 output_file 文件。-a是一个选项。

我现在想在 python 中分析 output_file 但想避免再次打开文件。所以我想将jarFile.jar作为 Python 函数运行,例如:

output=jarFile(input_file)

我已经安装了 JPype 并让它工作,我已经设置了类路径并启动了 JVM 环境:

import jpype

classpath="/home/me/folder/jarFile.jar"

jpype.startJVM(jpype.getDefaultJVMPath(),"-Djava.class.path=%s"%classpath)

我现在卡住了......

4

1 回答 1

0

java -jar jarFile.jar执行在 jar 的清单文件中配置的类文件的 main 方法。META-INF/MANIFEST.MF如果您提取 jar 文件(使用任何 zip 工具打开 jar),您会找到该类名。寻找 的值Main-Class。例如,如果是这样,com.foo.bar.Application您应该能够像这样调用 main 方法

def jarFile(input_file):
    # jpype is started as you already did
    assert jpype.isJVMStarted()
    tf = tempfile.NamedTemporaryFile()
    jpype.com.foo.bar.Application.main(['-a', input_file, tf.name])
    return tf

(我不确定tempfile模块的正确使用,请自行检查)

于 2014-10-09T15:56:58.123 回答