从Eclipse插件使用Jython
从Eclipse插件运行时,我很难让jython正常工作。我有一个简单的对象工厂,它加载符合Java接口的python模块。所有这些在独立模式下都能正常工作。但是,当我将其打包为eclipse插件时,我会根据一些变量得到不同的错误:
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:
鉴于我的java包是com.foo。
Given that my java package is com.foo.
1)如果我在没有修改任何路径的情况下运行,我得到:没有名为foo的模块
1) If I run without modifying any paths, I get: "No module named foo"
2)如果我然后使用以下命令将我的java jar添加到sys.path:
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..."));
我得到:
a)我的调用python模块的构造函数(在constr中显示打印)
b)我从调用 tojava 返回一个PySingleton。名称字段是错误。
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)此时,我尝试使Eclipse中的类路径与Standalone完全相同,因此我将我的jar添加到在调用python解释器之前运行时的classpath。
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.
我得到了我最喜欢的错误消息:SystemError:自动代理初始化应该仅在代理类上发生
I get my favorite error message: SystemError: Automatic proxy initialization should only occur on proxy classes
这个让我发疯。我对独立模式的速度感到印象深刻。在Eclipse下运行应该有那么大的不同?我认为它应该只是类路径的问题,但到目前为止,似乎不是这样。
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.
最后想出这一个。以下是我必须做的事情:
Finally figure this one out. Here is what I had to do:
1)我使用JSR223 ScriptEngine而不是PythonInterpreter:
1) I used the JSR223 ScriptEngine instead of PythonInterpreter:
engine.get(MODULE_NAME); //在类上获取模块
getConstructors [0] .newInstance(null)的类对象以获取对象
//将其强制转换为接口!
2)确保您的Eclipse插件未打包为jar(在3.5集Eclipse-BundleShape:dir中)
3)添加jython.jar以及你想要在Manifest中找到运行时类路径的模块的任何路径。
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.
希望这可以帮助某人。