如何从Jython调用由Java类执行的Java方法?

时间:2012-06-25 01:33:51

标签: java parent jython

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

我对如何处理这个问题毫无头绪。我已经做了数周的研究,没有回答我的问题;那就是:

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

----------------------------------- UPDATE ----------- ----------------------------------------

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

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()

这甚至可能吗?有办法做到这一点吗?

----------------------------------- UPDATE ----------- ----------------------------------------

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 off” “javac -classpath C:\ jython2.7a2 \ jython.jar * .java” “回声完成” “暂停> nul”

现在它甚至都没有编译...(我在代码中改变了一些东西,看它是否发生了变化,但它没有!)

2 个答案:

答案 0 :(得分:1)

需要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");
        }
    }
    

答案 1 :(得分:0)

是的,这种方式很好,但你不能在构造函数方法中运行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()
相关问题