Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 555
Luft Avatar asked Aug 15 '26 08:08

Luft


1 Answers

need to jython.jar

  1. execute python code in java.

    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.

    python script file, named test.py

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

    java code:

    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

    python script file, named test.py:

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

    java code:

    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");
        }
    }
    
like image 74
Liu guanghua Avatar answered Aug 17 '26 22:08

Liu guanghua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!