跨越编程语言界限(五)
java –jar jython-installer-2.7.0.jar
This will start the regular GUI installer on most systems, or a console installer on headless systems.
Jython Standalone mode Install安装
when you come to the "Installation type" page, select "Standalone “
配置环境变量
新增JYTHON_THOME的环境变量,并设置为安装路径。
配置classpath和path路径分别为%JYTHON_HOME%\Lib和%JYTHON_HOME%\bin
二、安装了jython后,通过jython xxx.py 的方式执行Python程序。
1、先调用Java版本的Python,Jython,即jython.jar,去解析python,
2、然后转换成java所支持的字节码
3、最终调用java中的JVM,去执行python代码的。
三、在java类中直接执行python语句
import org.python.util.PythonInterpreter;
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");
interpreter.exec("print days[1];");
}
}
四、在java中执行python文件
1、建立一个python脚本,名字为:my_utils.py
def adder(a, b):
return a + b
2、建立一个java文件,用来调用Python文件函数
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:/Workspaces/J2EE/Test/config/my_utils.py");
PyFunction func = (PyFunction) interpreter.get("adder", PyFunction.class);
int a = 2010, b = 6;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}
}
建立一个java文件,用来调用Python文件
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:/Workspaces/J2EE/Test/config/my_utils.py");
PyFunction func = (PyFunction) interpreter.get("adder", PyFunction.class);
int a = 2010, b = 6;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}
}
五、在java中执行python文件
1、建立java类,调用这个脚本
import org.python.util.PythonInterpreter;
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:/Workspaces/J2EE/Test/config/input.py");
}
}
2、建立java类,调用这个脚本
import org.python.util.PythonInterpreter;
public class FirstJythonScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("D:/Workspaces/J2EE/Test/config/input.py");
}
}
六、通过JPype执行java语句
1、创建Python文件
from jpype import *
startJVM(getDefaultJVMPath(), "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()
2、通过调用jar包
先创建Java文件Jpype.java
package com.jpype;
public class JPype {
public String sayHello(String user){
return "hello " + user;
}
public int calc(int a, int b){
return a + b;
}
}
注意:一定要创建测试文件测试java包可用。
再创建Python文件调用JPype.jar
from jpype import *
import os.path
jarpath = os.path.join(os.path.abspath('.'), 'D:/python/Jython/JPype/out/artifacts/JPype_jar/')
startJVM(getDefaultJVMPath(),"-ea", "-Djava.class.path=%s" % (jarpath + 'JPype.jar'))
JDClass = JClass("com.jpype.JPype")
jd = JDClass()
jprint = java.lang.System.out.println
jprint(jd.sayHello("waw"))
jprint(jd.calc(2,4))
shutdownJVM()
更多推荐
所有评论(0)