如何从嵌入式 Jython 中调用 java 函数?

发布于 2024-11-07 01:44:57 字数 785 浏览 2 评论 0原文

我创建了一个 PythonInterpreter 对象,并且想要调用 java 函数,但不断收到错误:

Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'jytest2' is not defined
Java Result: 1

How do you call a java function from a live running system?

public static void main(String args[])
    {      
        ModRet modRet = new ModRet();
        jytest();
    }

public void jytest()
    {
        PythonInterpreter interp = new PythonInterpreter();

        interp.exec("print \'Hello; jython has successfully been embedded!\'");
        interp.exec("print " + FPS);
        interp.exec("jytest2()");

    }

    public void jytest2()
    {
        System.out.println("HIHIHI");
    }

I have created an PythonInterpreter object, and want to call a java function but keep getting the error:

Exception in thread "main" Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'jytest2' is not defined
Java Result: 1

How do you call a java function from a live running system?

public static void main(String args[])
    {      
        ModRet modRet = new ModRet();
        jytest();
    }

public void jytest()
    {
        PythonInterpreter interp = new PythonInterpreter();

        interp.exec("print \'Hello; jython has successfully been embedded!\'");
        interp.exec("print " + FPS);
        interp.exec("jytest2()");

    }

    public void jytest2()
    {
        System.out.println("HIHIHI");
    }

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

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

发布评论

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

评论(2

花想c 2024-11-14 01:44:57

我通常发现我需要使用对象工厂模式并通过接口调用事物,如 Jython Book - 第 10 章

我仍然不太清楚您想要完成的任务,所以我没有任何代码适合您;但我相信你会发现这本书很有帮助。

I generally find that I need to use the Object Factory pattern and invoke things via an Interface as described in the Jython Book - Chapter 10

I'm still not super-clear on what you're trying to accomplish, so I don't have any code for you; but I'm sure you'll find the book helpful.

过期以后 2024-11-14 01:44:57

默认情况下,当前类路径包含在 jython 路径中。要访问路径中的任何方法,您应该执行导入。

package com.mycompany.jythontest;


import org.python.util.PythonInterpreter;


public class App 
{

  public static void sayHello(String hello) {
    System.out.println(hello);
  }

  public static void main( String[] args )
  {
      PythonInterpreter py = new PythonInterpreter();

      py.exec("from com.mycompany.jythontest import App");
      py.exec("App.sayHello('hello')");
  }
}

如果您想访问 jython 中的 java 实例,可以使用 py.set(string, object) 使其在上下文中可访问。 (就像 javax.scripting 一样)

      App app = new App();
      py.set("appinstance", app);
      py.exec("appinstance.sayHi('hello world')");

By default, the current classpath is included in jython path. To access any method in the path, you should do an import.

package com.mycompany.jythontest;


import org.python.util.PythonInterpreter;


public class App 
{

  public static void sayHello(String hello) {
    System.out.println(hello);
  }

  public static void main( String[] args )
  {
      PythonInterpreter py = new PythonInterpreter();

      py.exec("from com.mycompany.jythontest import App");
      py.exec("App.sayHello('hello')");
  }
}

And if you want to access a java instance in jython, you can use py.set(string, object) to make it accessible in the context. (Just like javax.scripting)

      App app = new App();
      py.set("appinstance", app);
      py.exec("appinstance.sayHi('hello world')");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文