Java调用Python脚本工具类
2017-03-28 16:16
731 查看
[本文出自天外归云的博客园]
在网上查了很多方法都不成功,在google上搜到一篇文章,做了一些小修改,能够处理中文输出。提取一个运行python脚本的Java工具类如下:
Java中调用方法如下:
其中script_path需替换为Python脚本路径,args替换为向脚本传递的参数。
在网上查了很多方法都不成功,在google上搜到一篇文章,做了一些小修改,能够处理中文输出。提取一个运行python脚本的Java工具类如下:
package com.autox.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class RunPython { public static ArrayList<String> run_py(String script) { String s = null; ArrayList<String> result = new ArrayList<String>(); ArrayList<String> error = new ArrayList<String>(); try { Process p = Runtime.getRuntime().exec(script); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK")); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((s = stdInput.readLine()) != null) { result.add(s); } while ((s = stdError.readLine()) != null) { error.add(s); } if (error != null) { System.out.println(error); } } catch (IOException e) { e.printStackTrace(); } return result; } }
Java中调用方法如下:
ArrayList<String> result = RunPython.run_py("python script_path args"); for (String item : result) { System.out.println(item); }
其中script_path需替换为Python脚本路径,args替换为向脚本传递的参数。
相关文章推荐
- java直接调用python脚本的例子
- Java调用Python脚本
- java调用python脚本文件大盘点
- java动态传参调用python脚本
- java 直接调用python脚本,并传递参数
- java 调用python的工具类
- Java调用Python脚本
- JAVA 与 Python 脚本间相互调用实例
- java调用python脚本传参
- java 利用Runtime.getRuntime().exec()调用python脚本并传参
- Java调用Python脚本
- java直接调用python脚本的例子
- 在java中调用python脚本
- java调用mysql的工具类实现数据库远程备份,还原,生成创建表的sql脚本
- java调用python脚本方法
- Java调用Python脚本
- java 调用python,shell脚本
- 使用Runtime.getRuntime().exec()在java中调用python脚本
- java 利用Runtime.getRuntime().exec()调用python脚本并传参
- Java调用Python脚本并获取返回值