简体   繁体   English

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

[英]How do I call Java Methods from Jython that is executed by the Java class?

I am a novice programmer in (Java/C++/C#) and I also know Python. 我是(Java / C ++ / C#)的新手程序员,我也了解Python。 I am trying to create a GameEngine in Java that can call Jython scripts, that have access to methods in the Java engine. 我正在尝试用Java创建一个可以调用Jython脚本的GameEngine,该脚本可以访问Java引擎中的方法。

I am clueless as to how to approach this. 我对如何解决这个问题一无所知。 I have already done weeks of research and nothing has answered my question ; 我已经做了数周的研究,没有任何回答我的问题; that is: 那是:

How can I call methods in my Parent class, from my JythonScript, which is executed by my Parent class? 如何从父类执行的JythonScript中调用父类中的方法?

-----------------------------------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--------------------------------------------------- -----------------------------------更新-------------- -------------------------------------

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" Jython的位置:“ C:\\ jython2.7a2 \\ jython.jar”我的工作的位置:“ C:\\ Documents and Settings \\ PC \\ Desktop \\ Jython * .java”我的本地JtyhonJar的位置:“ C:\\ Documents and设置\\ PC \\桌面\\ Jython \\ jython.jar”

my compiler I wrote: "@echo off" "javac -classpath C:\\jython2.7a2\\jython.jar *.java" "echo done" "pause >nul" 我写的编译器:“ @echo off”“ javac -classpath C:\\ jython2.7a2 \\ jython.jar * .java”“ echo完成”“暂停> nul”

now it doesn't even compile... (I've changed little things in my code to see if it changed and it hasn't!) 现在它甚至不编译...(我在代码中做了一些小改动,以查看它是否已更改,并且没有更改!)

need to jython.jar 需要jython.jar

  1. execute python code in java. 在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. invoke python script method in java. 在Java中调用python脚本方法。

    python script file, named test.py python脚本文件,名为test.py

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

    java code: 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. run python script in java 在Java中运行python脚本

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

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

    java code: 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"); } } 

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. 是的,这种方法很好,但是您不能在构造函数方法中运行python脚本,如果是这样,它将在您的代码中完全失效。 please see the following code. 请参见以下代码。 you run PythonScriptTest class, it will run python script first, then python script will invoke PythonScriptTest.SpawnPlayer() method. 您运行PythonScriptTest类,它将首先运行python脚本,然后python脚本将调用PythonScriptTest.SpawnPlayer()方法。

java code: 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 scripts, named util.py: 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM