完整封装java执行脚本工具——ProcessBuilder

简述

概念

ProcessBuilder类用于创建操作系统进程,它提供一种启动和管理进程的方法。每个 ProcessBuilder 实例管理一个进程属性集。它的start() 方法利用这些属性创建一个新的 Process 实例。start() 方法可以从同一实例重复调用,以利用相同的或相关的属性创建新的子进程。

Process抽象类有以下6个抽象方法:

  • destroy()
    杀掉子进程。
  • exitValue()
    返回子进程的出口值。
  • InputStream getErrorStream()
    获得子进程的错误流。
  • InputStream getInputStream()
    获得子进程的输入流。
  • OutputStream getOutputStream()
    获得子进程的输出流。
  • waitFor()
    导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。

java代码实现

封装工具类

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

/**
 * @Author :feiyang
 * @Date :Created in 4:49 PM 2019/9/26
 */
public class ShellUtils {
    
    

    private static ProcessBuilder processBuilder = new ProcessBuilder();

    /**
     * 执行脚本命令
     * @author  feiyang
     * @param commend
     * @return  java.lang.Process
     * @date    2019/9/26
     * @throws
     */
    public static Process exec(List<String> commend) {
    
    
        Process process = null;
        try {
    
    
            String[] commends = new String[commend.size()];
            commend.toArray(commends);
            processBuilder.command(commends);
            process = processBuilder.start();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return process;
    }

    /**
     * 
     * @author  feiyang 
     * @param process
     * @return  java.lang.String 
     * @date    2019/9/26
     * @throws
     */
    public static String getOutput(Process process) {
    
    
        String output = null;
        BufferedReader reader = null;
        try {
    
    
            if (process != null) {
    
    
                StringBuffer stringBuffer = new StringBuffer();
                reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                while (reader.read() != -1){
    
    
                    stringBuffer.append("\n" + reader.readLine());
                }
                output = stringBuffer.toString();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        closeQuietly(reader);
        return output;
    }

    /**
     * 获取错误信息
     * @author  feiyang
     * @param process
     * @return  java.lang.String
     * @date    2019/9/26
     * @throws
     */
    public static String getError(Process process) {
    
    
        String errput = null;
        BufferedReader reader = null;
        try {
    
    
            if (process != null) {
    
    
                StringBuffer stringBuffer = new StringBuffer();
                reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                while (reader.read() != -1){
    
    
                    stringBuffer.append("\n" + reader.readLine());
                }
                errput = stringBuffer.toString();
            }
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        closeQuietly(reader);
        return errput;
    }

    /**
     * 关流
     * @author  feiyang
     * @param reader
     * @return  void
     * @date    2019/9/26
     * @throws
     */
    public static void closeQuietly(Reader reader) {
    
    
        try {
    
    
            if (reader != null) {
    
    
                reader.close();
            }
        } catch (IOException ioe) {
    
    
            ioe.printStackTrace();
        }
    }

    /**
     * 终止进程
     * @author  feiyang
     * @param process
     * @return  void
     * @date    2019/9/26
     * @throws
     */
    public static void destroy(Process process) {
    
    
        if (process != null) {
    
    
            process.destroyForcibly();
        }
    }
}

测试

public static void main(String[] args) {
    
    
        List<String> commend = new ArrayList<>();
        commend.add("java");
        commend.add("-version");
        Process process = ShellUtils.exec(this.commend);
        String message = ShellUtils.getError(process);
        String error = ShellUtils.getError(process);
        ShellUtils.destroy(process);
    }

猜你喜欢

转载自blog.csdn.net/qq_42133100/article/details/102722870
今日推荐