【发布时间】:2021-01-30 03:41:31
【问题描述】:
我正在尝试解决应该是一个简单的问题,但我错过了一些东西。我有一个非常简单的 Java 类:
package blah.blah;
public class Tester {
public String testMethod1() { return "GotHere"; }
public String testMethod2() { return "GotHereToo"; }
} // Tester
我正在尝试让它加载到 JPype:
import jpype
import jpype.imports
from jpype.types import *
path_to_jvm = "C:\\Java\\OracleJDK-8_241_x64\\jre\\bin\\server\\jvm.dll"
jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\eclipse-workspace\\JpypeTest\\src;"])
from java.lang import System
print(System.getProperty("java.class.path"))
from java.io import ObjectInputStream
from blah.blah import Tester
tester = Tester()
print(tester.testMethod1())
print(tester.testMethod2())
jpype.shutdownJVM()
当我使用上面的代码直接加载类时,一切正常。当我尝试让它通过 JAR 加载 Class 文件时,它停止工作。
jpype.startJVM(path_to_jvm, classpath=["C:\\Users\\Administrator\\JpypeTest.jar;"])
我得到的错误是:
Traceback (most recent call last):
File ".\jpype_test.py", line 16, in <module>
from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'
PS C:\Users\Administrator> python .\jpype_test.py
C:\Users\Administrator\JpypeTest.jar;
Traceback (most recent call last):
File ".\jpype_test.py", line 16, in <module>
from blah.blah import Tester
ModuleNotFoundError: No module named 'blah'
我使用 JAR 导出从 Eclipse 2020/09 和命令行使用以下几种不同方式构建了 JAR 文件:
C:\Users\Administrator\eclipse-workspace\JpypeTest\src>"C:\Java\OracleJDK-8_241_x64\bin"\jar cf C:\Users\Administrator\JpypeTest.jar blah\blah\*.class
我已经确认 JVM 和 python 解释器都是 64 位的。我也尽了最大努力确保 Eclipse、命令行和 Python 之间的 JVM 完全相同。
据我所知,无论我如何构建它,JAR 看起来都很好。它包含 blah\blah 目录及其下的 Tester.class 文件。清单是 JAR 中唯一的其他文件。
我还尝试使用不同目录级别的类文件创建 JAR 文件(即,一级 blah 目录,没有一级 blah 目录)。
以下是python软件的版本:
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Package Version
------------ -------------------
certifi 2020.6.20
JPype1 1.0.2
pip 20.2.3
setuptools 50.3.0.post20201006
wheel 0.35.1
wincertstore 0.2
很明显,我的基础管道工作正常,因为它可以看到类文件。关于 JAR 为何失败的任何想法?
非常感谢您的考虑。
【问题讨论】: