gpt4 book ai didi

java - 从 Eclipse 插件使用 Jython

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:59 24 4
gpt4 key购买 nike

当从 Eclipse 插件运行时,我很难让 jython 正常工作。我有一个简单的对象工厂,它加载一个符合 Java 接口(interface)的 python 模块。所有这些在独立模式下都能正常工作。但是,当我将其打包为 eclipse 插件时,我会根据一些变量得到不同的错误:

假设我的 java 包是 com.foo。

1) 如果我在不修改任何路径的情况下运行,我得到:“没有名为 foo 的模块”

2) 如果我然后使用以下方法将我的 java jar 添加到 sys.path:

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

我得到:

a) 我的 python 模块的构造函数被调用(显示 constr 中的打印)
b) 我从对 tojava 的调用中得到一个返回的 PySingleton。名称字段是“错误”。

3) 此时,我尝试使 Eclipse 中的类路径与 Standalone 中的类路径完全相同,因此我在调用 python 解释器之前在运行时将我的 jar 添加到类路径中。

我收到我最喜欢的错误消息:SystemError: Automatic proxy initialization should only occur on proxy classes

这个让我发疯。我对在独立模式下完成这项工作的速度印象深刻。在 Eclipse 下运行应该有那么大的不同吗?我认为这应该只是类路径的问题,但到目前为止,似乎并非如此。

最佳答案

终于弄明白了。这是我必须做的:

1) 我使用了 JSR223 ScriptEngine 而不是 PythonInterpreter:

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

2) 确保您的 Eclipse 插件未打包为 jar(在 3.5 set Eclipse-BundleShape: dir 中)
3) 将 jython.jar 和您想要在其中定位模块的任何路径添加到 list 中的运行时类路径。

希望这对某人有帮助。

关于java - 从 Eclipse 插件使用 Jython,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1765802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com