java调用python脚本
Jython(原JPython),是一个用Java语言写的Python解释器。
在没有第三方模块的情况下,通常选择利用Jython来调用Python代码,
它是一个开源的JAR包,你可以到官网下载
一个HelloPython程序
importorg.python.util.PythonInterpreter;
publicclassHelloPython{
publicstaticvoidmain(String[]args){
PythonInterpreterinterpreter=newPythonInterpreter();
interpreter.exec("print('hello')");
}
}
什么是PythonInterpreter?它的中文意思即是“Python解释器”。我们知道Python程序都是通过解释器来执行的,我们在Java中创建一个“解释器”对象,模拟Python解释器的行为,通过exec("Python语句")直接在JVM中执行Python代码,上面代码的输出结果为:hello
在Jvm中执行Python脚本
interpreter.execfile("D:/labs/mytest/hello.py");
如上,将exec改为execfile就可以了。需要注意的是,这个.py文件不能含有第三方模块,因为这个“Python脚本”最终还是在JVM环境下执行的,如果有第三方模块将会报错:javaImportError:Nomolenamedxxx
仅在Java中调用Python编写的函数
先完成一个hello.py代码:
defhello():
return'Hello'
在Java代码中调用这个函数:
importorg.python.core.PyFunction;
importorg.python.core.PyObject;
importorg.python.util.PythonInterpreter;
publicclassHelloPython{
publicstaticvoidmain(String[]args){
PythonInterpreterinterpreter=newPythonInterpreter();
interpreter.execfile("D:/labs/hello.py");
PyFunctionpyFunction=interpreter.get("hello",PyFunction.class);//第一个参数为期望获得的函数(变量)的名字,第二个参数为期望返回的对象类型
PyObjectpyObject=pyFunction.__call__();//调用函数
System.out.println(pyObject);
}
}
上面的代码执行结果为:Hello
即便只是调用一个函数,也必须先加载这个.py文件,之后再通过Jython包中所定义的类获取、调用这个函数。
如果函数需要参数,在Java中必须先将参数转化为对应的“Python类型”,例如:
__call__(newPyInteger(a),newPyInteger(b))
a,b的类型为Java中的int型,还有诸如:PyString(Stringstring)、PyList(Iterator
2. java调用python出现乱码怎么解决
java调Python返回结果出现乱码解决:
Python脚本编码格式已经用了utf-8,但是返回还是乱码,说明是java这边传过去编码有问题。
1、Python编码格式:
2、一般我们中文文本编码格式常用的有:UTF-8,GBK,GB2312。分别试一下就知道了:
utf-8不行!!!
BGK可以!
GB2312也可以!
发现了!utf-8是国际通用的编码,而我们国家就是GB2312和GBK,gb2312是简体中文,gbk包含简体和繁体。而我们电脑是windows系统,windows默认的编码方式是GBK,所有直接用GBK就可以解决问题了!
众多python培训视频,尽在python学习网,欢迎在线学习!
3. Java运行Python脚本的几种方式
由于在项目需要执行Python,找寻相关资料,总结出以下几种方式:
直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();
3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();
4. java调用python程序问题
Strings;
Processprocess=Runtime.getRuntime().exec("/usr/bin/pythonmy.py");
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(process.getInputStream());
while((s=bufferedReader.readLine())!=null){
System.out.println(s);
}
process.waitfor();
5. 怎么在java的flink中调用python程序
一、在java类中直接执行python语句
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
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];");
}// main
}
调用的结果是Tue,在控制台显示出来,这是直接进行调用的。
二、在java中调用本机python脚本中的函数
首先建立一个python脚本,名字为:my_utils.py
def adder(a, b):
return a + b
然后建立一个java类,用来测试,
java类代码 FirstJavaScript:
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\my_utils.py");
PyFunction func = (PyFunction) interpreter.get("adder",
PyFunction.class);
int a = 2010, b = 2;
PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b));
System.out.println("anwser = " + pyobj.toString());
}// main
}
得到的结果是:anwser = 2012
三、使用java直接执行python脚本
建立脚本inputpy
#open files
print 'hello'
number=[3,5,2,0,6]
print number
number.sort()
print number
number.append(0)
print number
print number.count(0)
print number.index(5)
建立java类,调用这个脚本:
import org.python.util.PythonInterpreter;
public class FirstJavaScript {
public static void main(String args[]) {
PythonInterpreter interpreter = new PythonInterpreter();
interpreter.execfile("C:\\Python27\\programs\\input.py");
}// main
}
得到的结果是:
hello
[3, 5, 2, 0, 6]
[0, 2, 3, 5, 6]
[0, 2, 3, 5, 6, 0]
2
3
6. 怎么使用java运行python脚本
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();
3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();
本回答由电脑网络分类达人 朱冬梅推荐
评论
1 0
瓶邪
采纳率:92% 来自团队:网络知道电脑团 擅长: 电脑/网络 生活 电子数码
其他回答
如果是jython,也就是运行在Jvm上的python的话,可以使用JSR223,JDK1.6已经包含了该扩展包。JSR223是一个用于解析多种脚本语言的库包,其中包括Jython。除了JSR223包之外,还需要jython-engine.jar包。
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
try
{
engine.eval(new FileReader("./script/listing.py"));
}
catch(ScriptException se)
{
}
catch(IOException ie)
{
}
7. 怎么使用java运行python脚本
1.直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();
3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();
8. 建立java与python的接口,让java能调用python脚本。请问可以用什么方式
不建议研究jython。比较简单的思路是把python脚本完全当做一个外部程序,用shell方式调用它。
首先设计好python脚本的接口,把参数用命令行方式传入,然后输出打印出来。示例:
$ python func.py arg1 arg2
result
然后在java里就可以用Runtime来执行shell命令,解析输出字符串然后得到结果。
9. 用java执行python
1.直接执行Python脚本代码
引用 org.python包
1 PythonInterpreter interpreter = new PythonInterpreter();
2 interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); "); ///执行python脚本
2. 执行python .py文件
1 PythonInterpreter interpreter = new PythonInterpreter();
2 InputStream filepy = new FileInputStream("D:\\demo.py");
3 interpreter.execfile(filepy); ///执行python py文件
4 filepy.close();
3. 使用Runtime.getRuntime()执行脚本文件
这种方式和.net下面调用cmd执行命令的方式类似。如果执行的python脚本有引用第三方包的,建议使用此种方式。使用上面两种方式会报错java ImportError: No mole named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py");
2 proc.waitFor();
10. eclipse中 java程序怎样调用python
方法如下:
package com.lyz.test.jython;
import org.python.util.PythonInterpreter;
/**
* 第一个Jython程序
* @author liuyazhuang
*
*/
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];");
}
}