Java|Java 执行CMD命令或执行BAT批处理方式
Java 执行CMD命令或执行BAT批处理
背景
日常开发中总能遇到一些奇怪的需求,例如使用java执行cmd命令或者bat批处理文件,今天就简单记录一下使用过程。
使用
废话不多说直接上代码
import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class Cmder {/*** 执行一个cmd命令** @param cmdCommand cmd命令* @return 命令执行结果字符串,如出现异常返回null*/public static String executeCmdCommand(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).append(" "); }return stringBuilder.toString(); } catch (Exception e) {e.printStackTrace(); return null; }}/*** 执行bat文件,** @param filebat文件路径* @param isCloseWindow 执行完毕后是否关闭cmd窗口* @return bat文件输出log*/public static String executeBatFile(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).append(" "); }return stringBuilder.toString(); } catch (Exception e) {e.printStackTrace(); return null; }}/*** 执行bat文件,新开窗口** @param filebat文件路径* @param isCloseWindow 执行完毕后是否关闭cmd窗口* @return bat文件输出log*/public static String executeBatFileWithNewWindow(String file, boolean isCloseWindow) {String cmdCommand; if (isCloseWindow) {cmdCommand = "cmd.exe /c start" + file; } else {cmdCommand = "cmd.exe /k start" + file; }StringBuilder stringBuilder = new StringBuilder(); Process process; try {process = Runtime.getRuntime().exec(cmdCommand); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line).append(" "); }return stringBuilder.toString(); } catch (Exception e) {e.printStackTrace(); return null; }}/*** 执行bat脚本** @param batScript 脚本内容* @param location脚本存储路径* @return 结果*/public static String executeBatScript(String batScript, String location) {StringBuilder stringBuilder = new StringBuilder(); FileWriter fw = null; try {//生成bat文件fw = new FileWriter(location); fw.write(batScript); fw.close(); } catch (IOException e) {e.printStackTrace(); }Process process; try {process = Runtime.getRuntime().exec(location); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = bufferedReader.readLine()) != null) {stringBuilder.append(line).append(" "); }return stringBuilder.toString(); } catch (Exception e) {e.printStackTrace(); return null; }}/*** 执行脚本,不停止,并输出执行结果** @param batScript 脚本内容* @param locationbat文件生成地址*/public void executeBatScriptAlways(String batScript, String location) {FileWriter fw = null; try {//生成bat文件fw = new FileWriter(location); fw.write(batScript); fw.close(); } catch (IOException e) {e.printStackTrace(); }StringBuilder stringBuilder = new StringBuilder(); //运行bat文件Process process; try {process = Runtime.getRuntime().exec(location); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK")); String line; while ((line = bufferedReader.readLine()) != null) {System.out.println(line); }} catch (Exception e) {e.printStackTrace(); }}}
Java 执行系统命令 1. windows
【Java|Java 执行CMD命令或执行BAT批处理方式】1.1 cmd
第一种方法
File dir = new File("D:\\mysql57\\mysql-5.7.29-winx64\\bin"); // String command="netstat -an"; String command = "c:\\windows\\system32\\cmd.exe /c mysqlbinlog../data/master-bin.000006 | more"; Runtime r = Runtime.getRuntime(); Process p = r.exec(command, null, dir); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuffer sb = new StringBuffer(); String inline; while (null != (inline = br.readLine())) {sb.append(inline).append("\n"); }System.out.println(sb.toString());
第二种方法
try {File dir = new File("D:\\mysql57\\mysql-5.7.29-winx64\\bin"); //此处是指定路径String[] cmd = new String[] { "cmd", "/c","mysqlbinlog -v ../data/master-bin.000006 | more"}; // cmd[2]是要执行的dos命令System.out.println(cmd[2]); Process process = Runtime.getRuntime().exec(cmd,null,dir); // 记录dos命令的返回信息StringBuffer resStr = new StringBuffer(); // 获取返回信息的流InputStream in = process.getInputStream(); Reader reader = new InputStreamReader(in); BufferedReader bReader = new BufferedReader(reader); for (String res = ""; (res = bReader.readLine()) != null; ) {resStr.append(res + "\n"); }System.out.println(resStr.toString()); bReader.close(); reader.close(); process.getOutputStream().close(); // 不要忘记了一定要关} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace(); }
windows cmd 命令
后台运行
start /b 程序名字start /b redis-server.exe redis.windows.conf
文章图片
关闭程序
taskkill /f /t /im 程序名字taskkill /f /t /im redis-server.exe
文章图片
查看进程 根据进程名称 查看进程
tasklist|find /i "redis-server.exe"
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- CVE-2020-16898|CVE-2020-16898 TCP/IP远程代码执行漏洞
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用