1

我是(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设置\PC\桌面\Jython\jython.jar"

我的编译器我写了:“@echo off”“javac -classpath C:\jython2.7a2\jython.jar *.java”“echo done”“pause >nul”

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

4

2 回答 2

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  
    

    爪哇代码:

    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  
    

    爪哇代码:

    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-06-25T02:49:13.377 回答
1

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

爪哇代码:

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()
于 2012-06-28T04:03:42.843 回答