在Python中实际使用Java类/ JAR?
作者:互联网
我花了大量时间寻找这个问题,并探索了许多解决方案.
这与此线程有关.
Calling Java from Python
最后,经过测试:
Pyjnius:无法在Windows中安装.
Py4J:可以在Windows上安装,但是使用Gateway有点繁琐.
JPype:5分钟内安装了Python 3,可以加载50Mo JAR,没有任何问题.
好消息是语法已与Python语法完全合并…
https://github.com/tcalmant/jpype-py3
只是想知道,是否有人开发了具有大型JAR的Python(即在生产服务器上运行)的Java实际包装应用程序?
解决方法:
为了节省很多人的时间,我发布了用于JPype的模块,可以很好地加载JAR.
import jpype as jp; import numpy as np; import os as os
jarpath= r"D:\zjavajar\\"
mavenurl= r"http://mvnrepository.com/artifact/"
# StartJVM (add "-Xmx" option with 1024M if crash due to not enough memory )
def importJAR(path1="", path2="", path3="", path4=""):
classpath = path1
if path2 != "": classpath = os.pathsep.join((classpath, path2))
if path3 != "": classpath = os.pathsep.join((classpath, path3))
if path4 != "": classpath = os.pathsep.join((classpath, path4))
jp.startJVM(jp.getJVMPath(),"-ea", "-Djava.class.path=%s" % classpath)
def showLoadedClass(): #Code to see the JAR loaded.
classloader = jp.java.lang.ClassLoader.getSystemClassLoader(); vv= [];
for x in classloader.getURLs(): vv.append(x.toString());
return vv
def loadSingleton(class1): single= jp.JClass(class1); return Single.getInstance()
def java_print(x): jp.java.lang.System.out.println(x) #Print in Java Console
标签:java,python,jar,jpype,pyjnius 来源: https://codeday.me/bug/20191012/1897961.html