shell java 执行

 

java shell 命令 

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2016年11月2日 下午1:54:49
 * desc:
 */
public class ShellProcess {

    private static ShellProcess instance;

    public static ShellProcess getInstance() {
        if (instance == null) {
            synchronized (ShellProcess.class) {
                if (instance == null) {
                    instance = new ShellProcess();
                }
            }
        }
        return instance;
    }

    /**
     * 执行相应shell脚本
     * @param args 执行脚本的参数,[0]path:shell脚本路径;[1~n]脚本入参
     * @return     返回码,0:成功  1:失败
     */
    public int runShell(String[] args) {
        int runRes = SystemGlobal.FAILED;
        try {
            Process process = Runtime.getRuntime().exec(args);//调用相应shell脚本
            new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
            new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
            runRes = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return runRes;
    }

    /**
     * 执行相应shell命令
     * @param cmd 执行的命令
     * @return    返回码,0:成功  1:失败
     */
    public int runShell(String cmd) {
        return runShell(cmd);
    }

    /**
     * 自动根据运行时环境执行shell命令
     * @param args shell命令以空格分割后的list
     * @return     返回码,0:成功  1:失败
     */
    public int runShell(List<String> args) {
        int runRes = SystemGlobal.FAILED;
        try {
            ProcessBuilder pb = new ProcessBuilder(args);
            Process process = pb.start();
            new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
            new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
            runRes = process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return runRes;
    }

    /**
     * 执行shell命令,并获取返回结果
     * @param args shell命令以空格分割后的list
     * @return     执行shell命令后的返回结果(按行分割后的list),如果发生异常,返回空List
     */
    public List<String> runShellWithResult(List<String> args) {
        List<String> results = new ArrayList<String>();
        try {
            ProcessBuilder pb = new ProcessBuilder(args);
            Process process = pb.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String data = null;
            while ((data = br.readLine()) != null) {
                results.add(data);
            }
            new StreamGobbler(process.getInputStream(), StreamGobbler.INFO).start();
            new StreamGobbler(process.getErrorStream(), StreamGobbler.ERROR).start();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<String>();
        }
        return results;
    }

}

 

 

 

package com.taskschedule.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;

/**
 * 
 * @author baoyou E-mail:curiousby@163.com
 * @version 2016年11月2日 下午1:54:57
 * desc:
 */
public class StreamGobbler extends Thread {
    private InputStream is;
    private int type;
    private static Logger logger = Logger.getLogger(StreamGobbler.class);

    public static final int INFO = 0;
    public static final int ERROR = 1;

    public StreamGobbler(InputStream is, int type) {
        this.is = is;
        this.type = type;
    }

    @Override
    public void run() {
        try {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (type == INFO) {
                    logger.info(line);
                } else if (type == ERROR) {
                    logger.error(line);
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。


shell java 执行shell java 执行shell java 执行
 
 
 谢谢您的赞助,我会做的更好!

 

 

上一篇:w3m--用ubuntu的命令行模式浏览网页


下一篇:Struts2--Helloworld