• 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.

    下载地址:https://pypi.python.org/pypi/JPype1
    帮助文档:http://jpype.readthedocs.io/en/latest/

    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()
  • 相关阅读:
    在Twrp下删除面具模块
    Windows之批量创建用户、组部署
    H3C之HDLC实验部署
    Linux之防火墙部署
    H3C之Telnet实验部署
    win10 远程桌面 ubuntu
    VMware 虚拟机开机黑屏
    计算机存储单位换算
    TextCNN代码实践
    TextCNN论文解读
  • 原文地址:https://www.cnblogs.com/mumuli/p/5806963.html
Copyright © 2020-2023  润新知