• Java执行cmd命令工具类【转】


    Java执行cmd命令工具类【转】

    下述代码包含了java执行cmd、bat命令的代码

    package com.sinosoft.util;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    
    
    /**
     * @author rayfoo@qq.com
     * @version 1.0
     * <p>执行windows的cmd命令工具类</p>
     * @date 2021/9/1 17:20
     */
    public class CMDUtil {
    
        /**
         * 执行一个cmd命令
         * @param cmdCommand cmd命令
         * @return 命令执行结果字符串,如出现异常返回null
         */
        public static String excuteCMDCommand(String cmdCommand)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(cmdCommand);
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(process.getInputStream(), "GBK"));
                String line = null;
                while((line=bufferedReader.readLine()) != null)
                {
                    stringBuilder.append(line+"
    ");
                }
                return stringBuilder.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        /**
         * 执行bat文件,
         * @param file bat文件路径
         * @param isCloseWindow 执行完毕后是否关闭cmd窗口
         * @return bat文件输出log
         */
        public static String excuteBatFile(String file, boolean isCloseWindow)
        {
            String cmdCommand = null;
            if(isCloseWindow)
            {
                cmdCommand = "cmd.exe /c "+file;
            }else
            {
                cmdCommand = "cmd.exe /k "+file;
            }
            StringBuilder stringBuilder = new StringBuilder();
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(cmdCommand);
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(process.getInputStream(), "GBK"));
                String line = null;
                while((line=bufferedReader.readLine()) != null)
                {
                    stringBuilder.append(line+"
    ");
                }
                return stringBuilder.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 执行bat文件,新开窗口
         * @param file bat文件路径
         * @param isCloseWindow 执行完毕后是否关闭cmd窗口
         * @return bat文件输出log
         */
        public static String excuteBatFileWithNewWindow(String file, boolean isCloseWindow)
        {
            String cmdCommand = null;
            if(isCloseWindow)
            {
                cmdCommand = "cmd.exe /c start"+file;
            }else
            {
                cmdCommand = "cmd.exe /k start"+file;
            }
            StringBuilder stringBuilder = new StringBuilder();
            Process process = null;
            try {
                process = Runtime.getRuntime().exec(cmdCommand);
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(process.getInputStream(), "GBK"));
                String line = null;
                while((line=bufferedReader.readLine()) != null)
                {
                    stringBuilder.append(line+"
    ");
                }
                return stringBuilder.toString();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
    
    

    转自:JAVA执行cmd命令(包含执行批处理文件)

  • 相关阅读:
    openfl使用64位的ndk时,编译报错的问题!
    Haxe是何物?
    jsp中如何判断el表达式中的BigDecimal==0
    如何在springmvc的请求过程中获得地址栏的请求
    【原创】【滑块验证码】
    【原创】【aes加密】
    【原创】【qrcodejs2】生成二维码
    【原创】【ueditor】监听内容
    【原创】【ueditor】内容过多时 菜单控件遮挡页面
    js杂谈
  • 原文地址:https://www.cnblogs.com/zhangruifeng/p/15215608.html
Copyright © 2020-2023  润新知