Linux下安装配置jython环境:
0)安装好jdk环境
1)下载jython_installer-2.2rc2.jar路径:
2)java -jar jython_installer-2.2rc2.jar 图象界面安装,需要先设置好:export DISPLAY=*.*.*.*:0.0
java -jar jython_installer-2.2rc2.jar --console文本解面安装,需要选择和设置一些路径
3)export PATH=$PATH:/usr/java/jython
4)jython 就可以直接使用jython shell了
5)编写一个jython文件test.py(其中可以引用java类库),然后直接运行:jython test.py
from java.util import Random
rng = Random()
#This is a comment in Jython
print "Flipping a coin..."
if rng.nextBoolean():
print "Came up heads"
else:
print "Came up tails"
6)编写一个java原文件,hello.java,其中就可以直接调用jython的库的东西了。
编译前需要先设置如下环境变量
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class SimpleEmbedded {
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 a");
interp.exec("x = 2+2");
PyObject x = interp.get("x");
System.out.println("x: "+x);
System.out.println("Goodbye, cruel world");
}
}
javac SimpleEmbedded.java //编译
java SimpleEmbedded //运行
最后打印出结果:
Hello, brave new world
sys module
42
x: 4
Goodbye, cruel world
7)怎样打包成jar文件
jar cmf CLIParser.mf ../CLIParser.jar ConsoleInputStream.class com/utstar/cliparser/*.class
其中mf文件内容:
Manifest-Version: 1.0
Main-Class: com/utstar/cliparser/CLIParser
Class-Path: ./lib/parser.jar ./lib/jaxp.jar ./lib/jython.jar ./lib/MIBCompiler.jar ./lib/manager.jar ./lib/TelnetClient.jar
阅读(1822) | 评论(0) | 转发(0) |