Question

I have read and searched all stack overflow .. I also found JPype class not found but it didn't help me although it is solved! I have the same problem ! I am using Mac , python 2.7.6

My both python code and A.java are on desktop. But I keep receiving this error :

Traceback (most recent call last): File "/Users/jeren/Desktop/aa.py", line 13, in A = jpype.JClass("A") File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jpype/_jclass.py", line 54, in JClass raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name) java.lang.ExceptionPyRaisable: java.lang.Exception: Class A not found

aa.py : import jpype

import os

jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=/Users/jeren/Desktop/")

A = jpype.JClass("A")

a = A()

jpype.shutdownJVM()

A.java :

class A

{

    public A()

    {

        super();

    }

    public String sayHi()

    {

        return("Hello");

    }



    public static void main(String[] argv)

    {

        System.out.println ("Hello ");

    }



    public static int add(int a, int b)

    {

        return(a+b);

    }

}

My mac , java and python are all 64bit ! where the problem can be?

Était-ce utile?

La solution

everything was ok just needed to add a 'public' to the beginning of class A:

    public class A
    {       
        public A()       
        {
            super();
        }
        public String sayHi()

        {
            return("Hello");
        }

Autres conseils

Here are some further nodes on specifying the class path for jpype.

A. Check JDK path

I had several versions of Java JDK installed and getDefaultJVMPath did not yield the expected path. I needed to replace

jpype.getDefaultJVMPath()

with the path to the JDK, that actually has been used to compile the code, e.g

D:/jdk11/bin/server/jvm.dll

B. relative paths It is possible to use relative paths. If my python file is for example in a package folder "pkg" and my java class file is in a sub folder "foo" of a "bin" folder:

parentFolder

  • pkg/main.py
  • bin/foo/Foo.class

    jpype.startJVM(jvmPath, '-Djava.class.path=../bin") link = jpype.JClass('foo.Foo')

For this example, the working directory of the java application will be the pkg folder. With other words, inside a main method of Foo class, you might want to use "../" to access the parentFolder.

C. -cp option does not work I tried to use -cp option instead of -Djava.class.path, which I would found more induitive. However, the following code does not work:

jpype.startJVM(jvmPath, '-cp', classPath)

D. jars need to be included individually

I tried to include a folder with several jar files.

parentFolder

  • foo/main.py
  • lib/foo.jar

Following code does not work:

jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')

Each jar file needs to be included individually, e.g.:

libOath = '../lib'
libJarPaths = str.join(';', [libPath + '/' + name for name in os.listdir(libPath)])
jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')

(Solution from JPype (Python): importing folder of jar's )

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top