-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseJython.java
More file actions
56 lines (48 loc) · 1.53 KB
/
UseJython.java
File metadata and controls
56 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PySystemState;
import org.python.util.PythonInterpreter;
public class UseJython {
private PythonInterpreter interpreter;
private PySystemState pySys;
public UseJython() {
interpreter = new PythonInterpreter();
pySys = Py.getSystemState();
}
public void execCode(String[] pythonCode) {
for (String str : pythonCode) {
interpreter.exec(str);
}
interpreter.cleanup();
}
public void execScriptNoReturn(String scriptPath, String[] args) {
for (String str : args) {
pySys.argv.append(new PyString(str));
}
interpreter.execfile(scriptPath);
interpreter.cleanup();
}
public void execScriptReturn(String scriptPath, String[] args) {
// System.setProperty("python.home",
// "C:\\Users\\Lovwin\\AppData\\Local\\Programs\\Python\\Python37");
PyList argvList = new PyList();
for (String str : args) {
argvList.append(new PyInteger(Integer.parseInt(str)));
}
interpreter.execfile(scriptPath);
PyFunction pyFunc = interpreter.get("add", PyFunction.class);
// 第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
// 调用Python程序中的函数
PyObject pyObj = pyFunc.__call__(argvList);
System.out.println("the result is: " + pyObj);
interpreter.cleanup();
}
public void close() {
interpreter.cleanup();
interpreter.close();
}
}