1. 新建工程,导入jython.jar,工程结构如图:
2. python脚本hello.py:
import time
print "Hello Jyhton!"
print time.asctime()
3. Java代码HelloJython.java:
package com.huey.dream.hello;
import org.python.util.PythonInterpreter;
/**
* Jython的简单示例
* @author huey2672
* @version 1.0
* @created 2014-9-19
*/
public class HelloJython {
public static void main(String[] args) {
PythonInterpreter pyInterpreter = new PythonInterpreter();
pyInterpreter.execfile("script/py/hello.py");
pyInterpreter.exec("print '2 ** 3 =', 2 ** 3");
}
}