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:mp 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:mp
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:mp>jpype_test.py
Traceback (most recent call last):
File "C:mpjpype_test.py", line 10, in <module>
A = jpype.JClass("A")
File "C:Python27libsite-packagesjpype\_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...
See Question&Answers more detail:
os