我使用 JPype 是为了在 python 中使用 java 类。 我有一个文件夹,其中包含多个自写的 .jar 文件。
我知道如何长期导入多个 .jar:
...
CLASSPATH = "/path/to/jars/first.jar:/path/to/jars/second.jar"
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % CLASSPATH)
MYLIB= jpype.JPackage("org").mylib
MyClass = MYLIB.MyClass
myObj = MyClass()
这很好用,但我认为可能有更好的方法。
我已经尝试过这个:
CLASSPATH = "/path/to/jars/*.jar"
还有这个:
CLASSPATH = "/path/to/jars/*"
在这两种情况下都会发生以下错误:
user@user:~/path/to/python/$ python test.py
Traceback (most recent call last):
File "test.py", line 23, in <module>
myObj = MyClass()
File "/usr/local/lib/python2.7/dist-packages/JPype1-0.6.2-py2.7-linux-x86_64.egg/jpype/_jpackage.py", line 60, in __call__
raise TypeError("Package {0} is not Callable".format(self.__name))
TypeError: Package org.mylib.MyClass is not Callable
我的问题:
有什么方法可以轻松导入 JPype 中包含多个 .jar 的文件夹吗?
最佳答案
您可以使用Python代码加入jar文件列表,无需硬编码
f'{str.join(":", ["path/to/jars/"+name for name in os.listdir("path/to/jars")])}'
关于java - JPype(Python): importing folder of jar's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44033891/