Python匯入jar包
阿新 • • 發佈:2018-11-08
一、環境準備
1.Python 3.6 64位+jre 64位+win64位(jre和Python位數一致)
2.安裝jpype,安裝的時候輸入 pip install jpype1
2.1安裝提示error: Microsoft Visual C++ 14.0 is required...
解決辦法:https://blog.csdn.net/tayhh/article/details/78601889
二、兩種呼叫jar包的方法
1.通過命令列
import subprocess
import chardet
import sys
class AES(object):
def __init__(self, data):
self.data = data
def encrypt(self):
command = "java -jar testImportJar.jar"
arg0 = self.data
cmd = [command,arg0]
new_cmd = " ".join(cmd)
stdout,stderr = subprocess.Popen(new_cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()
encoding = chardet.detect(stdout)["encoding"]
result = stdout.decode(encoding)
return result
if __name__ == '__main__':
data = sys.argv[0]
AES = AES("222")
print(AES.encrypt())
2.通過Jpype
import jpype
jvmPath = jpype.getDefaultJVMPath()
jvmPath = r'C:\Program Files\Java\jre1.8.0_65\bin\server\jvm.dll'#python 2.X用ur,Python 3.X用r
jpype.startJVM(jvmPath)
PWD = "&*($HJDGH4867%&T345386754OHYOH*^(ughiuR5fu&f&$KHAOS$&^%"
jpype.java.lang.System.out.println("hello")
jpype.shutdownJVM()