python调用java API

JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both virtual machines. Eventually, it should be possible to replace Java with python in many, though not all, situations. JSP, Servlets, RMI servers and IDE plugins are good candidates.

下载地址:https://pypi.python.org/pypi/JPype1
帮助文档:

1.测试代码 from jpype import * startJVM(getDefaultJVMPath(), "-ea") java.lang.System.out.println("Hello World") shutdownJVM() 2.引用jar包

在com目录下新建文件Test.java

package com; public class Test { public String run(String str){ return str; } }

编译

javac Test.java

打包
【论java的正确打包方式】必须把整个目录(报名和目录名要对应)打包,否则无法访问类。

jar cvf test.jar com

python调用

jarpath = os.path.join(os.path.abspath(\'.\'), \'libs/test.jar\') jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath) Test = jpype.JClass(\'com.Test\') # 或者通过JPackage引用Test类 # com = jpype.JPackage(\'com\') # Test = com.Test t = Test() res = t.run("a") print res jpype.shutdownJVM()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgdjwx.html