作者:-彼岸花开-hui | 来源:互联网 | 2023-05-17 00:16
怎么使用java运行python脚本引用org.python包1PythonInterpreterinterpreternewPythonInterpreter(;2interpr
怎么使用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 module named arcpy。
1 Process proc = Runtime.getRuntime().exec("python D:\\demo.py"); 2 proc.waitFor();本回答由电脑网络分类达人 朱冬梅推荐评论 1 0du瓶邪 采纳率:92% 来自团队:百度知道电脑团 擅长: 电脑/网络 生活 电子数码其他回答如果是jython,也就是运行在Jvm上的python的话,可以使用JSR223,JDK1.6已经包含了该扩展包。JSR223是一个用于解析多种脚本语言的库包,其中包括Jython。除了JSR223包之外,还需要jython-engine.jar包。
java怎么点用python脚本?
首先得声明一下,java是java,python是python,你用java得环境跑python这不是找麻烦吗,但是并不是说不行,java有一个Jpython得库,你可以下载一下,这方面原理设计jni技术,建议了解一下,如果单纯想运行一个脚本可以找Jpython得api文档看看
怎么使用java运行python脚本?
如果是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) { } 或者参考:http://www.360doc.com/content/10/0608/23/1404822_32043230.shtml很久之前用过ScriptEngine,对在Jvm上的脚本语言比如jruby,jython,groovy等支持性都很好,有点忘记了。
怎么在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,在控制台显示出来,这是直接进行调用的。