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
帮助文档:
在com目录下新建文件Test.java
package com; public class Test { public String run(String str){ return str; } }编译
javac Test.java打包
【论java的正确打包方式】必须把整个目录(报名和目录名要对应)打包,否则无法访问类。
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()