jython 中的 Java 类

发布于 2024-07-19 00:50:36 字数 409 浏览 4 评论 0原文

我需要从该脚本访问正在运行 jython 脚本的 java 类? 有什么帮助吗?

更新: 像这样的事情:

//JAVA CLASS
class Test{
     public String text;
     public Test
     {
        PythonInterpreter pi = new PythonInterpreter(null);
        pi.execfile("test.py");

     }

}

所以 int test.py 我需要做一些事情来改变测试类中文本的值

#test.py
doSomething()
Text.test = "new value"

希望它更清楚

I need to access the java class which is running jython script from that script?
Any help?

update:
Something like this:

//JAVA CLASS
class Test{
     public String text;
     public Test
     {
        PythonInterpreter pi = new PythonInterpreter(null);
        pi.execfile("test.py");

     }

}

So int test.py I need to do something to change the value of text in Test class

#test.py
doSomething()
Text.test = "new value"

Hope it is more clear

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

药祭#氼 2024-07-26 00:50:36

要将 java 类实例传递给 嵌入式 jython,您需要执行以下操作:

PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("a.test = 'new value'");

如果您想从外部脚本调用函数(将实例作为参数):

 PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("import externalscript");
    interp.exec("externalscript.function(a)");

To pass a java class instance to the embeded jython you need to do:

PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("a.test = 'new value'");

If you want to call a function (that takes the instance as an argument) from a external script:

 PythonInterpreter interp = new PythonInterpreter();
    interp.set("a", this);
    interp.exec("import externalscript");
    interp.exec("externalscript.function(a)");
心的憧憬 2024-07-26 00:50:36

您必须在 Jython 代码的顶部导入测试类。 我相信这将类似于

from com.examplepackage import Test

You'll also to set your text value as static, or pass the Java object into the Jython method。

请查看此处文章。

You have to import your test class at the top of your Jython code. I believe this would be something along the lines of

from com.examplepackage import Test

You'll also to set your text value as static, or pass the Java object into the Jython method.

Check out the article here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文