python调用java类库_python调用java API

JPype documentation

JPype is an effort to allow python programs full access to java class libraries. This is achieved not through re-implementing Python, as Jython/JPython has done, but rather through interfacing at the native level in both virtual machines. Eventually, it should be possible to replace Java with python in many, though not all, situations. JSP, Servlets, RMI servers and IDE plugins are good candidates.

1.测试代码

from jpype import *

startJVM(getDefaultJVMPath(), "-ea")

java.lang.System.out.println("Hello World")

shutdownJVM()

2.引用jar包

在com目录下新建文件Test.java

package com;

public class Test {

public String run(String str){

return str;

}

}

编译

javac Test.java

打包

【论java的正确打包方式】必须把整个目录(报名和目录名要对应)打包,否则无法访问类。

jar cvf test.jar com

python调用

jarpath = os.path.join(os.path.abspath('.'), 'libs/test.jar')

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath)

Test = jpype.JClass('com.Test')

# 或者通过JPackage引用Test类

# com = jpype.JPackage('com')

# Test = com.Test

t = Test()

res = t.run("a")

print res

jpype.shutdownJVM()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值