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

    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();
        }
    
    }

    本文来自博客园,作者:l-coil,转载请注明原文链接:https://www.cnblogs.com/l-coil/p/15138898.html

  • 相关阅读:
    gradle阿里云镜像配置
    tomcat相关
    Oracle通过SQL语句查看table所引用的对象(View/Function/Procedure/Trigger)
    C# 调用NPOI 修改Excel 完成实时更新公式结果
    SpringBoot2.1.6 整合CXF 实现Webservice
    SpringBoot中FreeMarker创建
    git回滚到指定commit
    idea push reject:push mater to origin/master was rejected by remote
    python读取文件
    python获取当前文件路径以及父文件路径
  • 原文地址:https://www.cnblogs.com/l-coil/p/15138898.html
Copyright © 2011-2022 走看看