Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式
Java 執(zhí)行CMD命令或執(zhí)行BAT批處理
背景
日常開發(fā)中總能遇到一些奇怪的需求,例如使用java執(zhí)行cmd命令或者bat批處理文件,今天就簡(jiǎn)單記錄一下使用過(guò)程。
使用
廢話不多說(shuō)直接上代碼
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class Cmder {
/**
* 執(zhí)行一個(gè)cmd命令
*
* @param cmdCommand cmd命令
* @return 命令執(zhí)行結(jié)果字符串,如出現(xiàn)異常返回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;
}
}
/**
* 執(zhí)行bat文件,
*
* @param file bat文件路徑
* @param isCloseWindow 執(zhí)行完畢后是否關(guān)閉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;
}
}
/**
* 執(zhí)行bat文件,新開窗口
*
* @param file bat文件路徑
* @param isCloseWindow 執(zhí)行完畢后是否關(guān)閉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;
}
}
/**
* 執(zhí)行bat腳本
*
* @param batScript 腳本內(nèi)容
* @param location 腳本存儲(chǔ)路徑
* @return 結(jié)果
*/
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;
}
}
/**
* 執(zhí)行腳本,不停止,并輸出執(zhí)行結(jié)果
*
* @param batScript 腳本內(nèi)容
* @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();
//運(yùn)行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 執(zhí)行系統(tǒng)命令
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]是要執(zhí)行的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(); // 不要忘記了一定要關(guān)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
windows cmd 命令
后臺(tái)運(yùn)行
start /b 程序名字 start /b redis-server.exe redis.windows.conf

關(guān)閉程序
taskkill /f /t /im 程序名字 taskkill /f /t /im redis-server.exe

查看進(jìn)程
根據(jù)進(jìn)程名稱 查看進(jìn)程
tasklist|find /i "redis-server.exe"
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中mkdir()和mkdirs()的區(qū)別及說(shuō)明
這篇文章主要介紹了Java中mkdir()和mkdirs()的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Java數(shù)據(jù)結(jié)構(gòu)之查找
本文主要介紹了Java數(shù)據(jù)結(jié)構(gòu)中查找的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
SpringBoot整合新版SpringSecurity完整過(guò)程
Spring Security是保障Spring應(yīng)用程序安全的強(qiáng)大框架,而新版的Spring Security引入了lambda表達(dá)式來(lái)配置,使得安全配置更加簡(jiǎn)潔、優(yōu)雅,本文將介紹如何在Spring Boot項(xiàng)目中整合新版Spring Security,需要的朋友可以參考下2024-02-02
啟動(dòng) Eclipse 彈出 Failed to load the JNI shared library jvm.dll
這篇文章主要介紹了有時(shí)候,新電腦上回碰到打開Eclipse時(shí),彈出提示“Failed to load the JNI shared library jvm.dll”錯(cuò)誤,這里給大家分享解決方案2016-08-08
web.xml中servlet, bean, filter, listenr 加載順序_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了web.xml中servlet, bean, filter, listenr 加載順序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

