Java调用Python测试成功的记录

来源:互联网 发布:数组和指针作为形参 编辑:程序博客网 时间:2024/06/07 08:49

需要的jar

<!-- python/jython --><dependency><groupId>org.python</groupId><artifactId>jython</artifactId><version>2.5.3</version></dependency><dependency><groupId>org.python</groupId><artifactId>jython-standalone</artifactId><version>2.7.0</version></dependency>




package com.python.test;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.util.ArrayList;import java.util.List;import org.python.core.PyFunction;import org.python.core.PyInteger;import org.python.core.PyObject;import org.python.util.PythonInterpreter;public class Test1 {public static void main(String[] args) throws InterruptedException, IOException {// python 此方法能够避免因为第三方库的原因造成的程序出错。 try { String[] str = {"python","C:\\Users\\orion\\Desktop\\测试开发\\\\python\\a.py","",""};            Process proc=Runtime.getRuntime().exec("python C:\\Users\\orion\\Desktop\\测试开发\\python\\a.py"); //执行py文件            InputStreamReader stdin=new InputStreamReader(proc.getInputStream());            LineNumberReader input=new LineNumberReader(stdin);            String line;            while((line=input.readLine())!=null ){            System.out.println(line);//得到输出            }        } catch (IOException e) {            e.printStackTrace();        }// python 此方法能够避免因为第三方库的原因造成的程序出错。// 执行python脚本 status/*PythonInterpreter interpreter = new PythonInterpreter();   interpreter.execfile("C:\\Users\\orion\\Desktop\\测试开发\\python\\a.py");  PythonInterpreter interpreter = new PythonInterpreter();   InputStream filepy = new FileInputStream("C:\\Users\\orion\\Desktop\\测试开发\\python\\a.py");  interpreter.execfile(filepy);  ///执行python py文件 filepy.close();*/// 执行python脚本 end// 直接调用python命令 status/*PythonInterpreter interpreter = new PythonInterpreter();    interpreter.exec("days=('mod','Tue','Wed','Thu','Fri','Sat','Sun'); ");    interpreter.exec("print days[1];");  */// 直接调用python命令 end// 传入参数  reurn返回值 status/*PythonInterpreter interpreter = new PythonInterpreter();          interpreter.execfile("C:\\Users\\orion\\Desktop\\测试开发\\python\\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());*/        // 传入参数  reurn返回值 end//String p = p("123123多少度");//System.out.println(p);}public static String p(String filename) {String result = "";        try {            Process process = Runtime.getRuntime().exec("python C:\\Users\\orion\\Desktop\\测试开发\\python\\p.py " + filename);//          process.waitFor();            InputStreamReader ir = new InputStreamReader(process.getInputStream());            LineNumberReader input = new LineNumberReader(ir);            result = input.readLine();            input.close();            ir.close();//            process.waitFor();        } catch (IOException e) {            //logger.error("调用python脚本并读取结果时出错:" + e.getMessage());        }        return result;}}


原创粉丝点击