Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

java - JPype class not found

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 my java sdk (including the javac bytecode compiler) was 64 bits. I uninstalled the java sdk and re-installed a 32bits version. Done! Solved!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...