Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式
Java 執(zhí)行CMD命令或執(zhí)行BAT批處理
背景
日常開(kāi)發(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文件,新開(kāi)窗口
*
* @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)文章
springboot tomcat的maxHttpFormPostSize參數(shù)示例解析
這篇文章主要介紹了springboot tomcat的maxHttpFormPostSize參數(shù)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
淺談xml配置spring profiles的幾個(gè)注意點(diǎn)
這篇文章主要介紹了淺談xml配置spring profiles的幾個(gè)注意點(diǎn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決
這篇文章主要介紹了Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
druid?handleException執(zhí)行流程源碼解析
這篇文章主要為大家介紹了druid?handleException執(zhí)行流程源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
MyBatis分頁(yè)插件PageHelper的使用與原理
提到插件相信大家都知道,插件的存在主要是用來(lái)改變或者增強(qiáng)原有的功能,MyBatis中也一樣,下面這篇文章主要給大家介紹了關(guān)于Mybatis第三方PageHelper分頁(yè)插件的使用與原理,需要的朋友可以參考下2023-02-02
SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼
本文主要介紹了SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Spring的refresh()方法相關(guān)異常解析
這篇文章主要介紹了Spring的refresh()方法相關(guān)異常解析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

