반응형
Windows 또는 Linux를 감지합니까? [복제]
이 질문에는 이미 답변이 있습니다.
Windows와 Linux 모두에서 공통 Java 프로그램을 실행하려고합니다.
프로그램은 각 플랫폼에서 다르게 수행해야합니다.
그렇다면 Java 프로그램이 Linux와 Windows에서 실행되고 있음을 어떻게 감지해야합니까?
apache commons lang 에는 다음과 같이 사용할 수있는 SystemUtils.java 클래스 가 있습니다.
SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS
시험:
System.getProperty("os.name");
http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties%28%29
유용한 간단한 클래스는 https://gist.github.com/kiuz/816e24aa787c2d102dd0 에 있습니다.
public class OSValidator {
private static String OS = System.getProperty("os.name").toLowerCase();
public static void main(String[] args) {
System.out.println(OS);
if (isWindows()) {
System.out.println("This is Windows");
} else if (isMac()) {
System.out.println("This is Mac");
} else if (isUnix()) {
System.out.println("This is Unix or Linux");
} else if (isSolaris()) {
System.out.println("This is Solaris");
} else {
System.out.println("Your OS is not support!!");
}
}
public static boolean isWindows() {
return OS.contains("win");
}
public static boolean isMac() {
return OS.contains("mac");
}
public static boolean isUnix() {
return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix"));
}
public static boolean isSolaris() {
return OS.contains("sunos");
}
public static String getOS(){
if (isWindows()) {
return "win";
} else if (isMac()) {
return "osx";
} else if (isUnix()) {
return "uni";
} else if (isSolaris()) {
return "sol";
} else {
return "err";
}
}
}
Java를 통해 프로그래밍 방식으로 실행중인 OS를 결정하기 위해 Apache lang 종속성을 사용하는 것이 가장 좋은 방법이라고 생각합니다
import org.apache.commons.lang3.SystemUtils;
public class App {
public static void main( String[] args ) {
if(SystemUtils.IS_OS_WINDOWS_7)
System.out.println("It's a Windows 7 OS");
if(SystemUtils.IS_OS_WINDOWS_8)
System.out.println("It's a Windows 8 OS");
if(SystemUtils.IS_OS_LINUX)
System.out.println("It's a Linux OS");
if(SystemUtils.IS_OS_MAC)
System.out.println("It's a MAC OS");
}
}
You can use "system.properties.os", for example:
public class GetOs {
public static void main (String[] args) {
String s =
"name: " + System.getProperty ("os.name");
s += ", version: " + System.getProperty ("os.version");
s += ", arch: " + System.getProperty ("os.arch");
System.out.println ("OS=" + s);
}
}
// EXAMPLE OUTPUT: OS=name: Windows 7, version: 6.1, arch: amd64
Here are more details:
참고URL : https://stackoverflow.com/questions/14288185/detecting-windows-or-linux
반응형
'Programming' 카테고리의 다른 글
팬 & 줌 이미지 (0) | 2020.07.13 |
---|---|
Boto3을 사용하여 S3 객체를 문자열로 열기 (0) | 2020.07.13 |
'ViewController'클래스에 신속한 초기화 기능이 없습니다. (0) | 2020.07.13 |
일정 시간이 지나면 웹 사이트 리디렉션 (0) | 2020.07.13 |
AJAX를 통해 배열을 mvc 동작으로 전달 (0) | 2020.07.13 |