java引用jpython的方法示例

时间:2022-08-31 21:09:21

本文实例讲述了java引用jpython的方法。分享给大家供大家参考,具体如下:

?
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
import java.util.ArrayList;
import java.util.List;
import org.python.core.PyException;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class python {
  public static void main(String []args)throws PyException
  {
    PythonInterpreter interp =new PythonInterpreter();
    System.out.println("Hello, brave new world");
    interp.exec("import sys");
    interp.exec("print sys");
    interp.set("a", new PyInteger(42));
    interp.exec("print 'test='+str(a)");
    interp.exec("x = 2+2");
    PyObject x = interp.get("x");
    System.out.println("x: "+x);
    System.out.println("Goodbye, cruel world");
    interp.exec("import re");
    //interp.exec("print dir(re)");
    interp.exec("t =re.compile('^(0{2}):(IFCSUM):(MANIFEST):')");
    interp.exec(" a = t.search('00:IFCSUM:MANIFEST:').groups()");
    PyObject k = interp.get("a");
    List list = ((List)k.__tojava__(List.class));
    // int ai= ((Integer)a.__tojava__(Integer.class)).intValue();
    System.out.println(k);
    System.out.println(list);
  }
}

希望本文所述对大家java程序设计有所帮助。