我试图打开一个jar文件并执行它的主要功能,但jpype抛出一个错误,没有任何意义了我。这里是我的代码:
jpype.startJVM(jpype.getDefaultJVMPath(), '-Djava.class.path="%s"' % jar)
CommandLine = jpype.JPackage('phylonet').coalescent.CommandLine
CommandLine.main(['-i', input_file, '-o', output_file])
jpype.shutdownJVM()
我得到这个错误:类型错误:包装phylonet.coalescent.CommandLine.main不赎回
我提供的jar文件的绝对路径,我已经得到了从META-INF / MANIFEST.MF主要功能:
cat tmp/META-INF/MANIFEST.MF | grep Main-Class
Main-Class: phylonet.coalescent.CommandLine
我试图打开的JAR文件被称为星空,从这里开始:https://github.com/smirarab/ASTRAL
调用它像这样按预期工作:
java -Djava.class.path="./astral.jar"
那么,为什么不,当我与jpype打电话了吗?
首先,我已经测试过我自己的jar文件的代码。事实上,我提出这样的错误:
TypeError: Package clip.frontend.Start.main is not Callable
然后,仔细阅读文档后,我用另一种方法。
import jpype
# I've used other set of parameters to JVM, and modified a bit your classpath setting.
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=clip.jar")
# Second difference, I decided to use JClass because it was more clear for me.
# Parameter array was kept empty.
jpype.JClass("clip.frontend.Start").main([])
jpype.shutdownJVM()
和输出是正确的:
% python2 main.py
2 2
+>+[<[>>+>+<<<-]>>[<<+>>-]>[[-]>>>>>>+<<<<<<<<<[-]>[-]>>>>>>>>[<<<<<<<<+>+>>>>>>>-]
<<<<<<<[>>>>>>>+<<<<<<<-]>>>>>>>[-]<<<<<<]<<<[>>+>+<<<-]>>[<<+>>-]>[[-]>>>>>>++
[<<<<<+>>>>>>>>>>>>+<<<<<<<-]<<<<<[>>>>>+<<<<<-]>>>>>>>>>>>>>[>>]+<<[<<]>[>[>>]
<+<[<<]>-]<<<<<<<[-]++[<<<<<+>>>>>>>>>>>>+<<<<<<<-]<<<<<[>>>>>+<<<<<-]>>>>>>>>>>>>>
[>>]+<<[<<]>[>[>>]<+<[<<]>-]<<<<<<<[-]#JVM has been shutdown
现在,我决定翻译我的解决方案,以配合您的问题:
import jpype
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=astral.jar")
jpype.JClass("phylonet.coalescent.CommandLine").main([])
jpype.shutdownJVM()
和代码工作正常。比实际的解决方案更重要的是,为什么没有你的代码工作。你用错了一组参数,并指定在classpath中的其他方式。
与JPackage上更换JCLASS,代码仍然有效。
import jpype
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=astral.jar")
jpype.JPackage('phylonet').coalescent.CommandLine.main([])
jpype.shutdownJVM()
当你的方式提取类从类路径是正确的,唯一可能的原因是指定无效的参数集。取出后-ea
的代码仍然有效,因此错误,你在这个代码片段制成的谎言。
'-Djava.class.path="%s"' % jar
而事实上,我反对我的答案,然后咣当使用这个,代码产生这样的:
TypeError: Package phylonet.coalescent.CommandLine.main is not Callable
这意味着,该参数包含以下内容:
-Djava.class.path="astral.jar"
而不是跟随
-Djava.class.path=astral.jar
该报价被放错位置,在结果引发的错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句