[关闭]
@torresdyl 2016-09-27T20:23:35.000000Z 字数 914 阅读 1558

[Java][OS] Detect All System type

java os


http://www.code4copy.com/java/detecting-os-type-in-java/

  1. public class OSInfo {
  2. public enum OS {
  3. WINDOWS, UNIX, POSIX_UNIX, MAC, OTHER
  4. }
  5. private static OS os = OS.OTHER;
  6. static {
  7. try {
  8. String osName = System.getProperty("os.name");
  9. if (osName == null) {
  10. throw new IOException("os.name not found");
  11. }
  12. osName = osName.toLowerCase(Locale.ENGLISH);
  13. if (osName.indexOf("windows") != -1) {
  14. os = OS.WINDOWS;
  15. } else if (osName.indexOf("linux") != -1
  16. || osName.indexOf("mpe/ix") != -1l
  17. || osName.indexOf("freebsd") != -1
  18. || osName.indexOf("irix") != -1
  19. || osName.indexOf("digital unix") != -1
  20. || osName.indexOf("unix") != -1) {
  21. os = OS.UNIX;
  22. } else if (osName.indexOf("mac os x") != -1) {
  23. os = OS.MAC;
  24. } else if (osName.indexOf("sun os") != -1
  25. || osName.indexOf("sunos") != -1
  26. || osName.indexOf("solaris") != -1) {
  27. os = OS.POSIX_UNIX;
  28. } else if (osName.indexOf("hp-ux") != -1
  29. || osName.indexOf("aix") != -1) {
  30. os = OS.POSIX_UNIX;
  31. } else {
  32. os = OS.OTHER;
  33. }
  34. } catch (Exception ex) {
  35. os = OS.OTHER;
  36. }
  37. }
  38. public static OS getOs() {
  39. return os;
  40. }
  41. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注