python调用javasdk -jpype
只是一个记录,大佬勿喷
批量加载jar依赖
# 获取jvm.dll 的文件路径
jvmPath = jpype.getDefaultJVMPath()
# 批量加载依赖jar
classPath="-Djava.class.path="
for path in os.listdir("../Lib/jar"):
classPath=classPath+"../Lib/jar/"+path+";"
print(classPath)
# 开启jvm
jpype.startJVM(jvmPath,"-ea", classPath)
具体代码如下
# 引入jpype模块
from jpype import *
import jpype
import os
import random
import base64
import array as arr
if __name__ == '__main__':
sigPub = base64.b64decode("3CaxD0QOOX9B+rKe73WVR06u4dVcdAfmPclXFcZnCu0Eren/OVtTnxKNt3f4lw7LxGACWc37As+v5JJ+Eih2g==")
sigPri = base64.b64decode("pzVi63eH8hcVVWzwnc1dapvLzpLyS6gOU73d2O0Hs0=")
cipPub = base64.b64decode("Fdyp7xCA1JWP8++BuTyWMY9qklOW/14xNpXXYsTWizXaXgQmfkWGQ3j8YBIG1cz5FTRj4+mvUydQunVafMXBA==")
cipPri = base64.b64decode("HhYCedbOX/CuHrGCVPoE2+hTXtlS1lZULYeUNV+LAM=")
ip = "localhost";
"""
基本的开发流程如下:
①、使用jpype开启jvm
②、加载java类
③、调用java方法
④、关闭jvm(不是真正意义上的关闭,卸载之前加载的类)
"""
# ①、使用jpype开启虚拟机(在开启jvm之前要加载类路径)
# 加载刚才打包的jar文件
# 获取jvm.dll 的文件路径
jvmPath = jpype.getDefaultJVMPath()
# 批量加载依赖jar
classPath="-Djava.class.path="
for path in os.listdir("../Lib/jar"):
classPath=classPath+"../Lib/jar/"+path+";"
print(classPath)
# 开启jvm
jpype.startJVM(jvmPath,"-ea", classPath)
# ②、加载java类(参数是java的长类名)
KmsClient = jpype.JClass("com.zyxx.kmssdk.client.KmsClient")
SysUser = jpype.JClass("com.zyxx.kmssdk.domain.SysUser")
UseKey = jpype.JClass("com.kms.jca.UseKey")
# KeyPair = jpype.JClass("java.security.KeyPair")
# 实例化java对象
kmsClient = KmsClient()
sysUser = SysUser()
useKey = UseKey()
map1 = java.util.HashMap()
# ③、调用java方法,由于我写的是静态方法,直接使用类名就可以调用方法
# javaClass.show()
sysUser.setDistinguishedName("sole-DN146")
sysUser.setUserName("test")
sysUser.setNickName("NickName")
# bytes = useKey.messageDigest(sigPub)
# for b in bytes:
# print(b)
# bytes1 = useKey.messageDigest(sigPri)
# bytes2 = useKey.messageDigest(cipPubStr)
# bytes3 = useKey.messageDigest(cipPriStr)
# keyPair = useKey.keyPairGenerator()
# print(keyPair.getPublic())
# print(keyPair.getPrivate())
# print("sigPub")
# for spu in sigPub:
# print(spu, end=" ")
# print("sigPri")
# for spr in sigPri:
# print(spr, end=" ")
# print("cipPub")
#
#
# for i,scu in cipPub:
# print(scu, end=" ")
# print("cipPri")
# for i,scr in cipPri:
# print(scr, end=" ")
# print(ip)
map1=kmsClient.register(ip, sigPub, sigPri, cipPub, cipPri, "3", sysUser)
print(map1)
# ④、关闭jvm
jpype.shutdownJVM()
pass