1
동일한 Java 응용 프로그램을 사용하는 Windows 시스템에서 Mac OS와 .bat 파일에 .sh를 실행할 수 있기를 원합니다. 이렇게하는 방법이 있습니까? 미리 감사드립니다. 운영 체제에 따라 .bat 파일과 .sh 파일을 별도로 실행하십시오.
동일한 Java 응용 프로그램을 사용하는 Windows 시스템에서 Mac OS와 .bat 파일에 .sh를 실행할 수 있기를 원합니다. 이렇게하는 방법이 있습니까? 미리 감사드립니다. 운영 체제에 따라 .bat 파일과 .sh 파일을 별도로 실행하십시오.
Java에서 OS를 찾기위한 내장 API가 있습니다.
System.getProperty("os.name");
.
그런 다음 case
또는 if else
을 쓸 수 있습니다. 마찬가지로 ...
private String os = System.getProperty("os.name").toLowerCase();
if (os.contains("windows"){
p = Runtime.getRuntime().exec("your batch file");
p.waitFor();
} else if (os.contains("mac"){
p = Runtime.getRuntime().exec("your sh file or dmg");
p.waitFor();
} else {
p = Runtime.getRuntime().exec("your .sh file");
p.waitFor();
}
/**
* @Reff http://www.tutorialspoint.com/java/lang/system_getproperties.htm
*/
import java.lang.String;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String os = System.getProperty("os.name").toString();
System.out.println("OS: " + os);
if("Windows 8".equals(os)) {
System.out.println("Windows 8");
} else {
System.out.println("Others");
}
}
}