2

I have the following Java file in Eclipse.

package java_python_tutorial;
import org.python.core.PyInstance;
import org.python.util.PythonInterpreter;

public class MainJython {
    public static void main(String[] args) {
        PythonInterpreter python = new PythonInterpreter();
        python.execfile("pytest/test_np.py");
//      PyInstance test = (PyInstance) python.eval("Test()");
//      test.invoke("printArr");
        python.close();
    }
}

If I include just the Jython JAR, running the file will result in an ImportError: no module named numpy from Python. I tried fixing this problem by also including the JyNI JAR in my project build path, but now running the file gives this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/python/modules/_weakref/ReferenceBackendFactory
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.newInstance(Class.java:412)
    at org.python.core.PySystemState.initialize(PySystemState.java:1015)
    at org.python.core.PySystemState.initialize(PySystemState.java:947)
    at org.python.core.PySystemState.initialize(PySystemState.java:930)
    at org.python.core.PySystemState.initialize(PySystemState.java:925)
    at org.python.core.PySystemState.initialize(PySystemState.java:920)
    at org.python.core.PySystemState.initialize(PySystemState.java:916)
    at org.python.core.ThreadStateMapping.getThreadState(ThreadStateMapping.java:32)
    at org.python.core.Py.getThreadState(Py.java:1440)
    at org.python.core.Py.getThreadState(Py.java:1436)
    at org.python.core.Py.getSystemState(Py.java:1456)
    at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:105)
    at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:94)
    at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:71)
    at java_python_tutorial.MainJython.main(MainJython.java:7)
Caused by: java.lang.ClassNotFoundException: org.python.modules._weakref.ReferenceBackendFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 18 more

For reference, the contents of the Python script I'm calling is:

import numpy as np

class TestNP(object):
    def __init__(self):
        self.arr = np.array([[1,2,3],[4,5,6]])

    def printArr(self):
        print(self.arr)
npCompleteNoob
  • 481
  • 1
  • 5
  • 15
  • Please share the versions of JyNI, Jython and NumPy you are using. The missing classes let me suspect that Jython 2.7.0 was used, but JyNI requires 2.7.1. NumPy was not supported before JyNI alpha 4 and even then is only supported in NumPy 1.12 and 1.13 series. If the right version does not fix it, please share how you configure classpath and pythonpath. – stewori Aug 04 '18 at 02:07
  • Did you fix this? I have same problem. – schoon Apr 23 '19 at 13:01
  • Unfortunately not. I ended up using a different solution. – npCompleteNoob Apr 27 '19 at 22:04

0 Answers0