Java调用python工具类

package com.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* @Author : liyang
* Description
* @Date : 16:34 2017/12/27
*/
public class RunPyUtil {

    public static String runCmd(String cmd) throws Exception {
        BufferedReader br = null;
        try {
            Process p = Runtime.getRuntime().exec(cmd);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            return sb.toString();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        try {
            String content = runCmd("python x.python ");
            System.out.println(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40990732/article/details/80827023