자바에서 Runtime.getRuntime().exec() 를 사용하여 windows 명령어를 실행하는 방법.

여기서 command는 실행 명령어 이다.

예 : move c:\data\source.txt c:\data\target.txt  <- 윈도우용

예 : mv /data/source.txt /data/target.txt <- 리눅스용


Process process;
String osName = System.getProperty("os.name");
String[] cmd;

if(osName.toLowerCase().startsWith("window")) {
    cmd = new String[] { "cmd.exe", "/y", "/c", command };
} else {
    cmd = new String[] { "/bin/sh", "-c", command };
}

process = Runtime.getRuntime().exec(cmd);

return process.waitFor();

[출처] http://blog.daum.net/lkflower/17224498  참고함.

OS 테스트 하기 위한 예제 소스

public class OS_Test {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String osArchitecture = System.getProperty("os.arch");
  String osName = System.getProperty("os.name");
  String javaHome = System.getProperty("java.home");
  String currentDirectory = System.getProperty("user.dir");
  String userHome = System.getProperty("user.home");
  // 32비트 인지 64비트인지 구별하기 위해 추가함
  String archDataModel = System.getProperty("sun.arch.data.model");
  
  System.out.println(osArchitecture);
  System.out.println(osName);
  System.out.println(javaHome);
  System.out.println(currentDirectory);
  System.out.println(userHome);
  System.out.println(archDataModel);
 }

}

 

+ Recent posts