Java 执行CMD命令或执行BAT批处理方式
作者:它山之石,可以攻玉
这篇文章主要介绍了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 file bat文件路径 * @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 file bat文件路径 * @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 location bat文件生成地址 */ 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
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 block e.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"
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。