【问题标题】:How do I call Java Methods from Jython that is executed by the Java class?如何从由 Java 类执行的 Jython 调用 Java 方法?
【发布时间】:2012-06-25 01:33:51
【问题描述】:

我是 (Java/C++/C#) 的新手程序员,我也知道 Python。我正在尝试用 Java 创建一个可以调用 Jython 脚本的 GameEngine,它可以访问 Java 引擎中的方法。

我不知道如何处理这个问题。我已经进行了数周的研究,但没有人回答我的问题;那就是:

如何从我的父类执行的 JythonScript 调用我的父类中的方法?

-----------------------更新----------- ------------------------------------------

好的,这里的答案帮助我理解了一些事情,但它并没有解决我的问题。 我想知道这样的事情是否可行:

class MyJavaClass
{
    Public MyJavaClass()
    {
        PythonInterpreter interp = new PythonInterpreter;
        interp.execfile("MyJythonScript.py");
        interp.exec("InGameCommand");
    }

    public void SpawnPlayer()
    {}

    public void KillPlayer()
    {}

}

MyJythonScript.py

Def InGameCommand():
    SpawnPlayer()
    KillPlayer()

这甚至可能吗?有办法吗?

-----------------------更新----------- ------------------------------------------

Jython 的位置:“C:\jython2.7a2\jython.jar” 我的工作位置:“C:\Documents and Settings\PC\Desktop\Jython*.java” 我本地 JtyhonJar 的位置:“C:\Documents and Settings\PC\Desktop\Jython\jython.jar”

我写的编译器: “@echo 关闭” “javac -classpath C:\jython2.7a2\jython.jar *.java” “回声完成” “暂停>无”

现在它甚至无法编译...(我在我的代码中更改了一些小东西,看看它是否改变了,但它没有!)

【问题讨论】:

  • 哦,如果它有助于澄清:Java 类包含 SpawnEntity() 类 Java 类执行 JythonScript Jython 脚本调用 SpawnEntity() 这基本上是我真正想要完成的 ^

标签: java parent jython


【解决方案1】:

是的,这种方式很好,但是你不能在构造方法中运行 python 脚本,如果是这样,它将在你的代码中死递归。请看下面的代码。你运行 PythonScriptTest 类,它会先运行 python 脚本,然后 python 脚本会调用 PythonScriptTest.SpawnPlayer() 方法。

java代码:

package com.xxx.jython;
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;

public class PythonScriptTest {

    public static void main(String[] args) {
        PythonScriptTest f = new PythonScriptTest();
        f.executePythonScript();
    }

    public PythonScriptTest(){
    }

    public void executePythonScript() {
            PythonInterpreter interpreter = new PythonInterpreter();
            interpreter.execfile("/home/XXX/XXX/util.py");
            PyFunction pyFuntion = (PyFunction) interpreter.get("InGameCommand", PyFunction.class);

            pyFuntion.__call__();
    }

    public void SpawnPlayer() {
            System.out.println("Run SpawnPlayer method ##################");
    }
}

Python 脚本,名为 util.py:

import sys.path as path
# the following path is eclipse output class dir
# does not contain java class package path.
path.append("/home/XXX/XXX/Test/bin")
from com.xxx.jython import PythonScriptTest

def add(a, b):  
    return a + b  

def InGameCommand():
    myJava = PythonScriptTest()
    myJava.SpawnPlayer()

【讨论】:

  • 哦,好的 :D 非常感谢!我只需要找到我的工作路径,它就是“AufWiedersehen!”针对这些问题!你摇滚,先生。
  • 我的脚本只是: import engine def InGameCommand(): myJava = engine() myJava.SpawnPlayer() Java 所做的就是: PythonInterpreter interpreter = new PythonInterpreter();解释器.execfile("MyScript.py"); PyFunction pyFuntion = (PyFunction) interpreter.get("InGameCommand", PyFunction.class); pyFuntion.__call__();执行时出现大量 python 错误(编译良好)
  • 哦,(对不起,这些问题很多)这应该像说明一样工作吗?到目前为止,所有这些都对我有用。除非我尝试更改在我的 Java 类中定义的变量,它看起来好像 jython 正在运行 java 的副本而不是回话......我可能做错了,但它不是 100% 对我来说。 X' 我知道的许多问题
【解决方案2】:

需要jython.jar

  1. 在java中执行python代码。

    import org.python.util.PythonInterpreter;  
    public class PythonScript{  
        public static void main(String args[]){  
            PythonInterpreter interpreter = new PythonInterpreter();  
            interpreter.exec("days=('One','Two','Three','Four'); ");  
            interpreter.exec("print days[1];");    
        }
    }  
    
  2. 在java中调用python脚本方法。

    python 脚本文件,名为 test.py

    def add(a, b):  
        return a + b  
    

    java代码:

    import org.python.core.PyFunction;  
    import org.python.core.PyInteger;  
    import org.python.core.PyObject;  
    import org.python.util.PythonInterpreter;  
    
    public class PythonScript {  
        public static void main(String args[]) {  
    
            PythonInterpreter interpreter = new PythonInterpreter();  
            interpreter.execfile("/home/XXX/XXX/test.py");  
            PyFunction pyFuntion = (PyFunction)interpreter.get("add",PyFunction.class);  
    
            int a = 10, b = 20 ;  
            PyObject pyobj = pyFuntion.__call__(new PyInteger(a), new PyInteger(b));  
            System.out.println("result = " + pyobj.toString());  
       }
    }  
    
  3. 在java中运行python脚本

    python 脚本文件,名为 test.py:

    number=[1,10,4,30,7,8,40]  
    print number  
    number.sort()  
    print number  
    

    java代码:

    import org.python.util.PythonInterpreter;
    
    public class FirstJavaScript {
    
        public static void main(String args[]) {
             PythonInterpreter interpreter = new PythonInterpreter();
             interpreter.execfile("/home/XXX/XXX/test.py");
        }
    }
    

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    相关资源
    最近更新 更多