Java调用Python测试成功的记录

需要的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;

	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值