Java – Using Jython From Eclipse Plugin

javajythonpython

I am having a tough time getting jython to work properly when run from an Eclipse plugin. I have a simple object factory that loads a python module conforming to a Java Interface. All of this works fine in standalone mode. However, when I package this as an eclipse plugin, I get a different error based on a few variables:

Given that my java package is com.foo.

1) If I run without modifying any paths, I get: "No module named foo"

2) If I then add my java jars to the sys.path using:

PythonInterpreter interp = new PythonInterpreter(null, new PySystemState());
PySystemState sys = Py.getSystemState();
sys.path.append(new PyString("myjar..."));

I get:

a) My python module's constructor gets called (print in the constr shows up)
b) I get a PySingleton returned from the call to tojava. The name field is "Error".

3) At this point, I try to make the classpath exactly the same in Eclipse as Standalone, so I add my jars to the classpath at runtime just before the python interpreter is called.

I get my favorite error message: SystemError: Automatic proxy initialization should only occur on proxy classes

This one is driving me crazy. I was impressed with how fast I got this going in standalone mode. Should running under Eclipse be that much different? I believe it should only be a matter of the classpath, but so far, that doesn't seem to be it.

Best Answer

Finally figure this one out. Here is what I had to do:

1) I used the JSR223 ScriptEngine instead of PythonInterpreter:

engine.get(module_name); //gets the class object of the module getConstructors[0].newInstance(null) on the class to get an object
//cast it to your interface!

2) Make sure your Eclipse plugin isn't packaged as a jar (in 3.5 set Eclipse-BundleShape: dir)
3) Add jython.jar and any paths where you want to locate modules to your Runtime Classpath in the Manifest.

Hope this helps someone.