Java에서 Windows DOS 명령을 호출하는 방법

1282 단어
이것은 자바 코드를 사용하여dos 명령을 호출하는 실례입니다. 여기서 저는 더 이상 말하지 않고 바로 코드를 올립니다. 코드는 다음과 같습니다.
 
  
import java.io.*;
/**
 *  Java windows DOS
 *  Windows ipconfig , IO 。
 */
public class RunWindowsCommand{
    public static void main(String[] args) {
        InputStream ins = null;
        String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" };  //
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            ins = process.getInputStream();  // cmd
            BufferedReader reader = new BufferedReader(new InputStreamReader(ins));  
            String line = null;  
            while ((line = reader.readLine()) != null) {  
                System.out.println(line);  //
            }
            int exitValue = process.waitFor();  
            System.out.println(" :" + exitValue); 
            process.getOutputStream().close();  //
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

좋은 웹페이지 즐겨찾기