java - JPype(Python): importing folder of jar's

标签 java python jpype

我使用 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/

相关文章:

java - 使用java jdbc在vista上连接mysql,通讯异常,尝试了常见的答案

java - 并发读取并从列表中删除

python - 为 Python 设置 Vim

python - 如何在 pylucene 3.5 中实例化 StandardAnalyzer? Version 对象在哪里?

java - JPy 与 Java 10

python - 无法在 ubuntu 12.04 64 位上安装 JPype

java - 卡夫卡Java客户端: ClassLoader doesn't find SASL/Scram Login Class

java - 如何从更新引擎服务获取更新状态?

python - ^ 在 python 中不表示行的开头

java - 在 JPype : package not callable error 中使用自定义 Java 类