JAVA cmd 명령 실행(일괄 처리 파일 포함)

3709 단어 자바
다음은 자바 가 cmd 명령 을 실행 하 는 코드 입 니 다.하나의 cmd 명령 을 실행 하거나 bat 파일 을 호출 하 는 방법 을 포함 합 니 다.
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 명령 의 각 문 구 를 입력 하면&&로 연결 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기