2012-06-25 40 views
1

我是(Java/C++/C#)的新手程序員,我也知道Python。我正在嘗試在Java中創建一個可以調用Jython腳本的GameEngine,它可以訪問Java引擎中的方法。如何從Java類執行由Jython調用Java方法?

我對如何解決這個問題毫無頭緒。我已經完成了數週的研究,沒有任何回答我的問題;那就是:

如何從我的Parent類執行的我的JythonScript中調用Parent類中的方法?

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

好的,這裏的答案幫助我理解了一些事情,但它並沒有解決我的問題。 我在想,如果因爲這樣的東西,例如將工作:

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

這甚至可能嗎?有辦法做到這一點?

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

Location to Jython:「 C:\ jython2.7a2 \ jython.jar「 我的工作位置:」C:\ Documents and Settings \ PC \ Desktop \ Jython * .java「 位置到我本地的JtyhonJar:」C:\ Documents and Settings \ PC \桌面\ Jython的\ jython.jar 「

我的編譯器我寫道: 」@迴響「 」的javac -classpath C:\ jython2.7a2 \ jython.jar *的.java「 」回聲做「 」暫停> nul「

現在它甚至不編譯...(我在我的代碼變化不大的東西,看它是否改變,現在也沒有!)

+0

哦,如果這有助於澄清: Java類包含SpawnEntity()類 Java類executs JythonScript Jython腳本調用SpawnEntity() 這基本上是我真正想要完成的^ – Luft

回答

0

是的,這樣是好的,但你不能在構造方法運行python腳本,如果是的話,它將在死遞歸你碼。請參閱下面的代碼。你運行PythonScriptTest類,它將首先運行python腳本,然後python腳本將調用PythonScriptTest.SpawnPlayer()方法。

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:

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

哦好吧:D非常感謝你!我只需要讓我的路徑工作這是「AufWiedersehen!」解決這些問題!先生,你搖滾。 – Luft

+0

我可以在運行jython的java類中操作變量嗎(被jython操作)? – Luft

+0

k..I終於得到了Jython的工作...現在我需要這個工作 – Luft

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

    Java代碼: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. 運行python腳本

    python腳本文件,命名爲測試。潘岳:

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

    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"); 
        } 
    } 
    
+0

謝謝您的回覆!我將不得不看看這個代碼,看看它是如何工作的,但是在此期間,我只能說,謝謝 :) – Luft