Python导入jar包

一、环境准备

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()


猜你喜欢

转载自www.cnblogs.com/lynnetest/p/9921156.html