我如何从Jython调用Java类执行的Java方法? [英] How do I call Java Methods from Jython that is executed by the Java class?

查看:59
本文介绍了我如何从Jython调用Java类执行的Java方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a novice programmer in (Java/C++/C#) and I also know Python. I am trying to create a GameEngine in Java that can call Jython scripts, that have access to methods in the Java engine.

我对如何解决这个问题一无所知.我已经做了数周的研究,没有任何回答我的问题;即:

I am clueless as to how to approach this. I have already done weeks of research and nothing has answered my question ; that is:

如何通过父类执行的JythonScript调用父类中的方法?

How can I call methods in my Parent class, from my JythonScript, which is executed by my Parent class?

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

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

好的,这里的答案帮助我了解了一些东西,但是并不能解决我的问题.我想知道这样的事情是否行得通:

Okay, The answer here helped me understand some things, but it didn't solve my problem. What I was wondering if something such as this would work:

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

    public void SpawnPlayer()
    {}

    public void KillPlayer()
    {}

}

MyJythonScript.py

MyJythonScript.py

Def InGameCommand():
    SpawnPlayer()
    KillPlayer()

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

Is this even possible? There a way to do this?

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

-----------------------------------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"

Location to Jython: "C:\jython2.7a2\jython.jar" Location to my work: "C:\Documents and Settings\PC\Desktop\Jython*.java" Location to my local JtyhonJar: "C:\Documents and Settings\PC\Desktop\Jython\jython.jar"

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

my compiler I wrote: "@echo off" "javac -classpath C:\jython2.7a2\jython.jar *.java" "echo done" "pause >nul"

现在它甚至无法编译...(我在代码中做了一些改动,以查看它是否已更改,并且没有更改!)

now it doesn't even compile... (I've changed little things in my code to see if it changed and it hasn't!)

推荐答案

是的,这种方法很好,但是您不能在构造函数方法中运行python脚本,如果这样的话,它将对您的代码无效.请参见以下代码.您运行PythonScriptTest类,它将首先运行python脚本,然后python脚本将调用PythonScriptTest.SpawnPlayer()方法.

Yes, this way is fine, but you can not run python script in constructor method, if so, it will be dead recursive at your code. please see the following code. you run PythonScriptTest class, it will run python script first, then python script will invoke PythonScriptTest.SpawnPlayer() method.

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:

Python scripts, named 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()

这篇关于我如何从Jython调用Java类执行的Java方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆