在Python中实际使用Java类/ JAR? [英] Practical use of Java class/JAR in Python?

查看:124
本文介绍了在Python中实际使用Java类/ JAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很多时间寻找这个并探索了很多解决方案。

I spent significant amount of time looking for this and explore many solutions.

这与这个线程有关。
从Python调用Java

This is related to this thread. Calling Java from Python

最后,经过测试:

Pyjnius:无法在Windows中安装。

Pyjnius : Cannot install in Windows.

Py4J:可以在Windows上安装,但使用Gateway有点重。

Py4J: can install on windows, but using Gateway is a bit heavy.

JPype: Python 3安装在5分钟后,可以加载50Mo JAR而不会出现任何问题。
好​​的是语法与Python语法完全合并...
https: //github.com/tcalmant/jpype-py3

JPype: Python 3 installed in 5 mins, can load 50Mo JAR without any issues. Good thing is the syntax is completely merged with Python syntax... https://github.com/tcalmant/jpype-py3

只是想知道,如果有人在Python中开发了真实的Java包装应用程序(即运行在大型JAR的生产服务器?

Just Wondering, if any people has developed real world wrapping application of Java in Python (ie running on a production server) with big size JAR ?

推荐答案

为了节省许多人的时间,我发布了用于JPype的模块,这正在加载JAR。

To save time to many people, I post the module I used for JPype, this is working nicel to load 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

这篇关于在Python中实际使用Java类/ JAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆