zoukankan      html  css  js  c++  java
  • 执行脚本工具类

    import lombok.extern.slf4j.Slf4j;
    import org.slf4j.Logger;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     * 获取脚本工具类
     *
     * @author l_coil
     * @date 2021-1-13
     */
    @Slf4j
    public class ExecuteScriptUtils {
    
        public static void main(String[] args) {
    
            String a = execute(new String[]{"bash","cd ../"});
            System.out.println(a);
        }
    
        /**
         * 执行脚本
         *
         * @return String
         */
        public static String execute(String instruction) {
            BufferedReader br = null;
            StringBuilder builder = new StringBuilder();
            try {
                Process p = Runtime.getRuntime().exec(instruction);
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String readLine = br.readLine();
                while (readLine != null) {
                    readLine = br.readLine();
                    builder.append(readLine == null ? "" : readLine);
                }
                log.info("readLine" + readLine);
                p.waitFor();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return builder.toString();
        }
    
    
        /**
         * 执行脚本
         *
         * @return String
         */
        public static String execute(String[] instruction) {
            BufferedReader br = null;
            StringBuilder builder = new StringBuilder();
            try {
                ProcessBuilder processBuilder = new ProcessBuilder(instruction);
    //            processBuilder.redirectErrorStream(true);
                Process p = processBuilder.start();
                br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String str;
                while ((str = br.readLine())!=null) {
                    builder.append(str);
                }
                p.waitFor();
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return builder.toString();
        }
    
    }
    博客园:https://www.cnblogs.com/xianquan
    Copyright ©2020 l-coil
    【转载文章务必保留出处和署名,谢谢!】
查看全文
  • 相关阅读:
    Java初学者:for循环介绍
    Java初学者:条件判断及其语句
    Java初学者:基本数据类型的强制类型转换
    eclipse+gradle+nodejs搭建web开发环境
    桑基图(sankey)
    tomcat性能优化
    数据库概览与选择
    在linux上装 postgresql 在 windows或 linux 连不上的问题的解决方法
    mosquitto的TLS功能测试,客户端使用paho.mqtt.golang(附JAVA版客户端实现)
    两步使用arm-linux-androideabi-addr2line定位JNI动态库中C代码错误位置
  • 原文地址:https://www.cnblogs.com/xianquan/p/15138898.html
  • Copyright © 2011-2022 走看看