# 调用jar包 import jpype #pip install jpype1 import os jarpath = os.path.join(os.path.abspath('.'), 'E://PycharmProjects//GUI//java//untitled.jar') #jar包路径 jvmPath = jpype.getDefaultJVMPath() #获得缺省值的jvmPath jpype.startJVM(jvmPath, "-ea", "-Djava.class.path=%s" % jarpath) #运行Jvm javaclass = jpype.JClass("com.Demo1") #加载java类 解压jar包之后就可以看到Demo1.class jc = javaclass() #实例化编译后的class文件一个对象 addsum = jc.add(3, 4) #调用类方法 print('两数之和为:', addsum) #打印调用方法 jpype.shutdownJVM() #关闭java虚拟机 ''' ''''''java代码'''''' package com; public class Demo1 { public int add(int a ,int b) { return a+b; } } '''
python调用java语言
调用java语句标签:调用,java,python,jar,jpype,class,jvmPath From: https://www.cnblogs.com/wangyafeng/p/17101457.html
import jpype #pip install jpype1
jvmPath = jpype.getDefaultJVMPath()
jpype.startJVM(jvmPath)
jpype.java.lang.System.out.println("hello world!")
jpype.shutdownJVM()
"""