Python 调用java代码

1、安装python库

 pip install jtypes.jpype

2、Test.java 代码

package com;
public class Test {
    public String run(String str){
        return str;
    }
}

3、生成jar包

javac Test.java
mkdir com
mv Test.class com
jar cvf test.jar com

4、Test.py

import os,jpype
jarpath = os.path.join(os.path.abspath('.'), 'test.jar')
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % jarpath)
Test = jpype.JClass('com.Test')
t = Test()
res = t.run("hello world")
print res
jpype.shutdownJVM()

5、运行

python Test.py

hello world
JVM activity report     :
        classes loaded       : 20
JVM has been shutdown

参考文章:
http://www.cnblogs.com/mumuli/p/5806963.html

你可能感兴趣的:(Python 调用java代码)