JPype下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype
pip install JPype1-0.6.3-cp36-cp36m-win_amd64.whl
1.测试代码
from jpype import *
if __name__ == '__main__':
startJVM("E:/JAVA/jdk1.8.0_92_64bit/jre/bin/server/jvm.dll", "-ea")
java.lang.System.out.println("Hello World")
shutdownJVM()
2.引用jar包
package com;
public class Test {
public String run(String str){
return str;
}
}
Python调用
from jpype import *
import os
if __name__ == '__main__':
jdkPath = "E:/JAVA/jdk1.8.0_92_64bit/jre/bin/server/jvm.dll"
jarpath = os.path.join(os.path.abspath('.'), 'F:/ceshi/test.jar')
startJVM(jdkPath, "-ea", "-Djava.class.path=%s" % jarpath)
Test = JClass('com.Test')
# 或者通过JPackage引用Test类
# com = jpype.JPackage('com')
# Test = com.Test
t = Test()
res = t.run("a")
print(res)
shutdownJVM()