JAVA cmd 명령 실행(일괄 처리 파일 포함)
3709 단어 자바
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* windows cmd
* @author dufei
*
*/
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;
}
}
}
명령
ipconfig
을 사용 하여 excuteCMDCommand 방법 을 테스트 합 니 다.주 함 수 는 다음 과 같 습 니 다.public static void main(String[] args) {
String cmd = "ipconfig";
String result = CMDUtil.excuteCMDCommand(cmd);
System.out.println(result);
}
결 과 는 다음 과 같다.
Windows IP
:
. . . . . . . . . . . . :
DNS . . . . . . . :
* 1:
. . . . . . . . . . . . :
DNS . . . . . . . :
* 2:
. . . . . . . . . . . . :
DNS . . . . . . . :
WLAN:
DNS . . . . . . . :
IPv6 . . . . . . . . : fe80::2598:4f7a:bc83:625c%3
IPv4 . . . . . . . . . . . . : 192.168.0.105
. . . . . . . . . . . . : 255.255.255.0
. . . . . . . . . . . . . : 192.168.0.1
2:
. . . . . . . . . . . . :
DNS . . . . . . . :
cmd 에서 실 행 된 결과 와 일치 합 니 다.한 번 에 여러 명령 을 실행 하려 면 cmd 명령 의 각 문 구 를 입력 하면&&로 연결 할 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.