Using Jython From Eclipse Plugin

痴心易碎 提交于 2019-12-21 20:35:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/1765802/using-jython-from-eclipse-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!