jpype2
Posted zhangzhaolin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jpype2相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
# @Time : 2020/5/21 0:04
# 从环境变量获取jvm虚拟机安装路径,若为None则获取默认路径
import os
import jpype
from jpype import *
e = jpype.getDefaultJVMPath()
print(e)
p = "-Djava.class.path=F:\codeTools\apache-jmeter-3.1\main.jar"
jpype.startJVM(e,p, **{"convertStrings": False})
# jpype.startJVM(e,p)
jpype.java.lang.System.out.println("helloworld")
jpype.java.lang.System.out.println("222")
javaClass = jpype.JClass(‘test.test03.Test‘)
javaInstance = javaClass()
# try {Test test = new Test();
# String s = test.CopyFile("d://1.txt","d://3.txt");
# } catch (IOException e) {
# e.printStackTrace();
# }
try:
s = javaInstance.CopyFile("d://1.txt", "d://5.txt")
print(s)
except Exception as e:
print("ERROR")
pass
# jvm_path = ‘‘
# if "JVM_PATH" in os.environ:
# jvm_path = os.environ["JVM_PATH"]
# print(jvm_path)
# else:
# jvm_path = jpype.getDefaultJVMPath()
# # 从环境变量获取Jar包路径,若为None则获取默认路径
# jar_path = ‘‘
# if "Jar_Path" in os.environ:
# jar_path = os.environ["Jar_Path"]
# else:
# # 获取当前的绝对路径
# jar_path = os.path.abspath(‘.‘)
#
# # 加载jar包
# jpype.startJVM(jvm_path, "-ea", "-Djava.class.path=%s" % (jar_path + ‘/Formula.jar‘))
#
# # 指定jar包的main class
# JDClass = jpype.JClass("com.xxx.formula.Calculator")
# # 创建类实例对象
# jd = JDClass()
‘‘‘
# 引用jar包类中的方法 execute,参数一是表达式,参数二是自变量x的值, cost_value应为70
cost_value = jd.execute(‘58+2*x‘, 6)
‘‘‘
# 关闭Java虚拟机,不写的话JVM会随程序执行完自动关闭
jpype.shutdownJVM()
以上是关于jpype2的主要内容,如果未能解决你的问题,请参考以下文章