使用JPype total newbie查询从Python调用jar文件
因此,我一直在使用subprocess.call从Python运行jar文件,如下所示:使用JPype total newbie查询从Python调用jar文件,python,jar,jpype,Python,Jar,Jpype,因此,我一直在使用subprocess.call从Python运行jar文件,如下所示: subprocess.call(['java','-jar','jarFile.jar',-a','input_file','output_file']) 将结果写入外部输出文件。和-a是一个选项 现在我想用python分析输出文件,但不想再次打开该文件。因此,我想将jarFile.jar作为Python函数运行,如: output=jarFile(input_file) 我已经安装了JPype并使其正
subprocess.call(['java','-jar','jarFile.jar',-a','input_file','output_file'])
将结果写入外部输出文件。和-a是一个选项
现在我想用python分析输出文件,但不想再次打开该文件。因此,我想将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)
现在我被卡住了…java-jar jarFile.jar执行在jar中配置的类文件的主方法。
如果您提取jar文件的META-INF/MANIFEST.MF,并使用任何zip工具打开jar,就会找到该类名。寻找Main类的值。例如,如果是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模块的正确用法,请检查您自己