如何从Java类执行由Jython调用Java方法?
我是(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“
现在它甚至不编译...(我在我的代码变化不大的东西,看它是否改变,现在也没有!)
是的,这样是好的,但你不能在构造方法运行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()
需要jython.jar
-
在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];"); } }
-
在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()); } }
-
运行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"); } }
谢谢您的回复!我将不得不看看这个代码,看看它是如何工作的,但是在此期间,我只能说,谢谢 :) – Luft
哦,如果这有助于澄清: Java类包含SpawnEntity()类 Java类executs JythonScript Jython脚本调用SpawnEntity() 这基本上是我真正想要完成的^ – Luft