java 调用python的工具类
来源:互联网 发布:java jar 指定main 编辑:程序博客网 时间:2024/06/18 02:40
</pre><pre name="code" class="java">package pythonUtil;/*** * pythonUtil 需要先导入相应的jpython jar 包 */import javax.script.*;import org.python.util.*;import java.io.*;import org.python.core.*;import static java.lang.System.*;public class pythonUtil {/** * 在python解释器中 执行一行命令 * * @param order * 执行的命令 例子:num=('1','2','3','4','5','6','7'); */public static void orderLine(String order) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.exec(order);}/** * * @param fileName * 要执行python文件 绝对路径 * @param functionName * 要执行的函数 * @param param1 * 整形参数1 * @param param2 * 整形参数2 */public static void exeFunction(String fileName, String functionName,int param1, int param2) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile(fileName);PyFunction func = (PyFunction) interpreter.get(functionName,PyFunction.class);PyObject pyobj = func.__call__(new PyInteger(param1), new PyInteger(param2));System.out.println(pyobj.toString());}/** * * @param fileName * 要执行python文件 绝对路径 * @param functionName * 要执行的函数 * @param param1 * 字符串参数 */public static void exeFunction(String fileName, String functionName,String param1) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile(fileName);PyFunction func = (PyFunction) interpreter.get(functionName,PyFunction.class);PyObject pyobj = func.__call__(new PyString(param1));System.out.println(pyobj.toString());}/** * 执行python script * * @param fileName * 绝对路径 */public static void exeScript(String fileName) {PythonInterpreter interpreter = new PythonInterpreter();interpreter.execfile(fileName);}/** * 利用JAVA中的Runtime来执行python文件 * @param 要执行的文件命令 */public static void RuntimeExePython(String order) {try {System.out.println("Execute Python File ");Process pr = Runtime.getRuntime().exec(order);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("Python File End");} catch (Exception e) {e.printStackTrace();}}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stuborderLine("print 1;");exeFunction("D:\\java语言编程程序\\python\\add.py", "add", 1, 2);exeFunction("D:\\java语言编程程序\\python\\add.py", "a", "hello world!");exeScript("D:\\java语言编程程序\\python\\add.py");RuntimeExePython("python add.py");}}
0 0
- java 调用python的工具类
- [java]Java调用python工具类(非jython)
- java调用kettle工具类
- java调用python的webservice
- java调用Python的方法
- Java 调用python的方法
- JAVA调用linux中的shell命令行的工具类
- Jpype python 调用JAVA 类
- java调用windows上的tail工具
- HproseUtils HPROSE java 调用别人接口的工具类 解决调用https接口问题
- arcgis中脚本工具调用公用的python脚本
- JAVA代码调用url链接工具类
- Java代码调用HTTP请求工具类
- java调用python的3种方式
- python调用java编写的Webservice
- python调用java编写的Webservice
- java中调用python的方法
- Java调用Python时的编码问题
- 记录一个神奇的Bug, -1 >= 2 ?
- qsort和sort学习与比较
- RGB、YUY2、YUYV、YVYU、UYVY、AYUV
- Android下的junit 单元测试
- maven项目警告: Using platform encoding (UTF-8 actually) to copy filtered resources
- java 调用python的工具类
- 34个漂亮的应用程序后台管理界面(系列一)
- 排序算法
- 【网页打印】网页打印时内容没有居中显示解决方法
- asp.net服务器控件的事件是怎样触发的
- 动态调用webservice
- hadoop课程大纲
- 工厂模式
- SWTBOK测试实践系列(4) -- 软件测试技术的黑白之道