zoukankan      html  css  js  c++  java
  • Java 执行系统命令工具类(commons-exec)

    依赖jar

            <!-- 可以在JVM中可靠地执行外部进程的库。 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-exec</artifactId>
                <version>1.3</version>
            </dependency>

    CommandUtils.java

    package utils;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    
    import org.apache.commons.exec.CommandLine;
    import org.apache.commons.exec.DefaultExecutor;
    import org.apache.commons.exec.ExecuteException;
    import org.apache.commons.exec.ExecuteWatchdog;
    import org.apache.commons.exec.PumpStreamHandler;
    
    /**
     * 执行系统命令工具类
     * 
     * @author Storm
     *
     */
    public class CommandUtils {
    
        private static final String DEFAULT_CHARSET = "GBK";
    
        /**
         * 执行指定命令
         * 
         * @param command 命令
         * @return 命令执行完成返回结果
         * @throws IOException 失败时抛出异常,由调用者捕获处理
         */
        public static String exeCommand(String command) throws IOException {
            try (
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
            ) {
                int exitCode = exeCommand(command, out);
                if (exitCode == 0) {
                    System.out.println("命令运行成功!");
                } else {
                    System.out.println("命令运行失败!");
                }
                return out.toString(DEFAULT_CHARSET);
            }
        }
    
        /**
         * 执行指定命令,输出结果到指定输出流中
         * 
         * @param command 命令
         * @param out 执行结果输出流
         * @return 执行结果状态码:执行成功返回0
         * @throws ExecuteException 失败时抛出异常,由调用者捕获处理
         * @throws IOException 失败时抛出异常,由调用者捕获处理
         */
        public static int exeCommand(String command, OutputStream out) throws ExecuteException, IOException {
            CommandLine commandLine = CommandLine.parse(command);
            PumpStreamHandler pumpStreamHandler = null;
            if (null == out) {
                pumpStreamHandler = new PumpStreamHandler();
            } else {
                pumpStreamHandler = new PumpStreamHandler(out);
            }
    
            // 设置超时时间为10秒
            ExecuteWatchdog watchdog = new ExecuteWatchdog(10000);
    
            DefaultExecutor executor = new DefaultExecutor();
            executor.setStreamHandler(pumpStreamHandler);
            executor.setWatchdog(watchdog);
    
            return executor.execute(commandLine);
        }
    
        public static void main(String[] args) {
            try {
                String result = exeCommand("ipconfig /all");
                System.out.println(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    }

     

     

    .

  • 相关阅读:
    强化学习的基本迭代方法
    基于文本描述的事务聚类
    学习强化学习之前需要掌握的3种技能
    其它 华硕 ASAU S4100U 系统安装 win10安装 重装系统 Invalid Partition Table 解决
    数据分析 一些基本的知识
    Python 取样式的内容 合并多个文件的样式 自定义样式
    电商 Python 生成补单公司需要的评论格式3
    SpringBlade 本地图片上传 生成缩略图
    SQL Server 字符串截取
    SpringBlade 本地图片上传
  • 原文地址:https://www.cnblogs.com/jonban/p/command.html
Copyright © 2011-2022 走看看