JPype is an amazing project since I allows to instantiate a JVM
directly from Python
.
Unfortunately, I got stuck in the first baby steps.
I have A.java
source code (located in C:\tmp folder
):
class A {
public A() {
super();
}
public String sayHi() {
return("Hello");
}
}
Which was compiled to a class, using: javac A.java
Thus, A.class is located in C:\tmp
folder.
I have the following Python
source code:
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
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.
Any clues?
EDIT 1:
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()
And returns 2.
EDIT 2:
Issue solved, see answer below...
I solved the problem and I would let the answer here for the records.
1) Nothing was wrong with the source code.
2) Problem was that my
Python
was 32 bits and myjava sdk
(including the javac bytecode compiler) was 64 bits. I uninstalled thejava sdk
and re-installed a 32bits version. Done! Solved!Try to change your path like this: