java 调用python的工具类

</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 stub
		orderLine("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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值