java调python脚本总结(一)
来源:互联网 发布:it was 编辑:程序博客网 时间:2024/04/30 00:47
最近需要使用java调python脚本,总结一下;
- 无参调用
如果要运行,都需要本地装python环境,尽量装python2.X版本,3.0貌似部分有改动(导致脚本运行不起来),装完python环境, 切入 CMD 输入python 即可测试下是否成功, 然后需要在你的eclipse 配置下 如图 (如果是应用服务器的话 在run connfig.. 中找到你运行的server ),
![这里写图片描述](http://img.blog.csdn.net/20171130093611985?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFvMTg3MzUwNDQ3OTE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
点击select 选出 path 即可(path中包含了 python的环境变量),
jython-standalone-2.7.0.jar 和jython-standalone-2.7.0源码 下载
// jython PythonInterpreter interpreter = new PythonInterpreter(); log.info(" start exec ... "); try { interpreter.execfile("D:/haocs/hello.py"); } catch (Exception e) { log.error("exec Exception ",e); } // java直接调用 private static final String PY_URL = "D:"+File.separator+"haocs"+File.separator+ "test.py"; public static void execPy() { Process proc = null; try { proc = Runtime.getRuntime().exec("python " + PY_URL); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
- 有参且有返回值
// python 解释器 仅限于方法普通参数,如果python 参数是数组则运行不了 /** * python 脚本如下 * def adder(a, b): * return a + b */ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile("D:\\haocs\\helloPython.py"); PyFunction func = (PyFunction) interpreter.get("adder", PyFunction.class); int a = 2010; int b = 2; PyObject pyobj = func.__call__(new PyInteger(a), new PyInteger(b)); System.out.println("result= " + pyobj.toString());// jython 数组调用python Properties props = new Properties(); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); String[] arguments ={"127.0.0.1", "demo-server","start"}; PythonInterpreter.initialize(props,System.getProperties(), arguments); PythonInterpreter interpreter = new PythonInterpreter(); interpreter.execfile(temp);---------- // java 调用python 单个参数如下 // String cmd="python D:\\labs\\hello.py "+name+" "+path; // Process pr = Runtime.getRuntime().exec(cmd); //java 调用python 数组参数 , String[] args = new String[] { "python", "D:\\labs\\client.py","127.0.0.1","demo-server","start"}; //设置命令行传入参数 Process pr = Runtime.getRuntime().exec(args); System.out.println(pr.toString()); BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); pr.waitFor(); System.out.println("end");
阅读全文
0 0
- java调python脚本总结(一)
- Jython-Java操作Python脚本学习笔记(一)
- python 脚本学习(一)
- Python 总结(一)
- selenium+python:脚本学习笔记(一)
- Python脚本练习一
- Python脚本总结--01
- python脚本错误总结
- SHELL 脚本命令总结(一)
- linux shell脚本攻略总结(一)
- Shell 脚本编程总结(一)
- shell脚本归纳总结(一)
- 总结下Python(一)
- python 多线程总结(一)
- (总结)python 基础知识一
- python学习总结(一)
- 学习python总结(一)
- python学习(总结一)
- grep不区分大小写查找字符串方法
- Google gflags使用说明
- 继承,super,向上造型
- 一些常用到的文件路径相关的API
- 关于PHP里面的$_ENV
- java调python脚本总结(一)
- c#中PropertyGrid 控件简介
- 动态规划--再论(数字三角形 poj1163)
- 中国高速铁路桥梁的设计控制指标
- Java程序员面试你必须要知道的10个JavaScript的技术点
- 用链表实现简单的通讯录功能
- Linux下设置svn开机自启动
- Netty学习小记
- linux 命令大全(收藏版)