Search code examples
javaparentjython

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. 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:

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

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

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"

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


Solution

  • 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 code:

    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:

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