找不到JPype类 [英] JPype class not found
问题描述
JPype是一个了不起的项目,因为我允许直接从 Python
实例化 JVM
。
JPype is an amazing project since I allows to instantiate a JVM
directly from Python
.
不幸的是,我在第一个小步骤中陷入困境。
Unfortunately, I got stuck in the first baby steps.
我有 A.java
源代码( 位于C:\ tmp文件夹
):
I have A.java
source code (located in C:\tmp folder
):
class A {
public A() {
super();
}
public String sayHi() {
return("Hello");
}
}
编译成一个类,使用: javac A.java
Which was compiled to a class, using: javac A.java
因此,A.class位于 C:\\ \\ tmp
文件夹。
Thus, A.class is located in C:\tmp
folder.
我有以下 Python
源代码:
import os
import jpype
jpype.startJVM(jpype.getDefaultJVMPath(), '-ea', '-Djava.class.path=c:\\tmp')
A = jpype.JClass("A")
a = A()
print a.sayHi()
jpype.shutdownJVM()
当我运行它时,我收到以下错误:
When I run it, I get the error below:
C:\tmp>jpype_test.py
Traceback (most recent call last):
File "C:\tmp\jpype_test.py", line 10, in <module>
A = jpype.JClass("A")
File "C:\Python27\lib\site-packages\jpype\_jclass.py", line 54, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name)
jpype._jexception.ExceptionPyRaisable: java.lang.Exception: Class A not found
由于我找不到A类,这可能是与CLASSPATH相关的问题,但我无法意识到我做错了什么。
Since I can't find the A class, it is probably an issue related to CLASSPATH, but I can't realize what I am doing wrong.
任何线索?
编辑1:
问题仍然存在。但是,只是添加到我的问题,如果我使用本机java库,如:java.util,代码运行WITHOUT错误。例如,下面的代码可以工作:
The problem persists. But, just to add to my question, if I use native java libraries, like: java.util, the code runs WITHOUT errors. For example, the code below works:
import jpype
jpype.startJVM(jpype.getDefaultJVMPath())
util = jpype.JPackage("java.util")
al = util.ArrayList()
al.add(1)
al.add(2)
print al.size()
jpype.shutdownJVM()
并返回2.
编辑2:
问题已解决,请参阅下面的答案......
Issue solved, see answer below...
推荐答案
我解决了问题,我会在这里给出记录的答案。
I solved the problem and I would let the answer here for the records.
1)源代码没有错。
2)问题是我的 Python
是32位,我的 java sdk
(包括javac字节码编译器)是64位。我卸载了 java sdk
并重新安装了32位版本。完成!解决了!
2) Problem was that my Python
was 32 bits and my java sdk
(including the javac bytecode compiler) was 64 bits. I uninstalled the java sdk
and re-installed a 32bits version. Done! Solved!
这篇关于找不到JPype类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!