【问题标题】:Calling a jar file from Python using JPype-total newbie query使用 JPype-total 新手查询从 Python 调用 jar 文件
【发布时间】:2014-11-28 15:58:48
【问题描述】:

所以我一直在使用 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)

现在卡住了……

【问题讨论】:

    标签: python jar jpype


    【解决方案1】:

    java -jar jarFile.jar 执行 jar 的 manifest file 中配置的类文件的 main 方法。 如果您提取 jar 文件的 META-INF/MANIFEST.MF(使用任何 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模块的正确使用,请自行检查)

    【讨论】:

      猜你喜欢
      • 2017-10-17
      • 2011-08-29
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 2013-02-21
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多