2018-11-26 python调用jar包模块
2018-11-26 本文已影响14人
昨天今天下雨天1
import jpype
import os
from platform import system
from utils.config import LIB_PATH
def get_jars():
# 此方法获取LIB_PATH下所有jar包并拼成字符串返回
jars = list()
for root, dirs, files in os.walk(LIB_PATH):
for f in files:
if os.path.splitext(f)[1] == '.jar':
jars.append(os.path.join(LIB_PATH, f))
sep = ';' if system() == 'Windows' else ':'
return sep.join(jars)
class JVM(object):
@staticmethod
def start_jvm(jar_path=None):
if not jpype.isJVMStarted():
# 如果没有传入jar包路径,则使用默认路径,添加LIB_PATH下所有jar包
if not jar_path:
jar_path = get_jars()
# 获取虚拟机地址
jvm_path = jpype.getDefaultJVMPath()
# 使用jvm调jar包,jar包传入相对路径
jpype.startJVM(jvm_path, "-ea", "-Djava.class.path=" + jar_path)
@staticmethod
def shutdown_jvm():
jpype.shutdownJVM()
if __name__ == '__main__':
j = get_jars()
# print(j)
JVM().start_jvm()
jpype.JClass('SimpleStringCypher')