Advertisement
timetraveller1992

Java Cross Platform Identification Code

Jun 26th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. public class NativeSystem {
  2.  
  3.     /**
  4.      * The operating system family enum.
  5.      *
  6.      * @see NativeSystem
  7.      */
  8.     public enum Family {
  9.         /** The FreeBSD operating system family. */
  10.         FREEBSD,
  11.  
  12.         /** The OpenBSD operating system family. */
  13.         OPENBSD,
  14.  
  15.         /** The Apple OS X operating system family. */
  16.         OSX,
  17.  
  18.         /** The Solaris operating system family. */
  19.         SOLARIS,
  20.  
  21.         /** The Linux operating system family. */
  22.         LINUX,
  23.  
  24.         /** The Windows operating system family. */
  25.         WINDOWS,
  26.  
  27.         /** Any unsupported operating system family. */
  28.         UNSUPPORTED
  29.     }
  30.  
  31.     /**
  32.      * The system architecture enum.
  33.      *
  34.      * @see NativeSystem
  35.      */
  36.     public enum Arch {
  37.         /** The alpha architecture. */
  38.         ALPHA,
  39.  
  40.         /** The arm architecture. */
  41.         ARM,
  42.  
  43.         /** The itanium64 32-bit architecture. */
  44.         IA64_32,
  45.  
  46.         /** The itanium64 architecture. */
  47.         IA64,
  48.  
  49.         /** The mips architecture. */
  50.         MIPS,
  51.  
  52.         /** The sparc architecture. */
  53.         SPARC,
  54.  
  55.         /** The sparc64 architecture. */
  56.         SPARC64,
  57.  
  58.         /** The ppc architecture. */
  59.         PPC,
  60.  
  61.         /** The ppc64 architecture. */
  62.         PPC64,
  63.  
  64.         /** The x86 architecture. */
  65.         x86,
  66.  
  67.         /** The amd64 architecture. */
  68.         x86_64,
  69.  
  70.         /** Any unsupported system architecture. */
  71.         UNSUPPORTED
  72.     }
  73.  
  74.     /**
  75.      * Determines the current operating system family.
  76.      *
  77.      * @return The current operating system family enum item.
  78.      */
  79.     public static Family getFamily() {
  80.         String osName = System.getProperty("os.name");
  81.         Family family;
  82.  
  83.         if (osName.equalsIgnoreCase("freebsd")) {
  84.             family = Family.FREEBSD;
  85.         }
  86.         else if (osName.equalsIgnoreCase("openbsd")) {
  87.             family = Family.OPENBSD;
  88.         }
  89.         else if (osName.equalsIgnoreCase("mac os x")) {
  90.             family = Family.OSX;
  91.         }
  92.         else if (osName.equalsIgnoreCase("solaris") ||
  93.                 osName.equalsIgnoreCase("sunos")) {
  94.             family = Family.SOLARIS;
  95.         }
  96.         else if (osName.equalsIgnoreCase("linux")) {
  97.             family = Family.LINUX;
  98.         }
  99.         else if (osName.toLowerCase().startsWith("windows")) {
  100.             family = Family.WINDOWS;
  101.         }
  102.         else {
  103.             family = Family.UNSUPPORTED;
  104.         }
  105.  
  106.         return family;
  107.     }
  108.  
  109.     /**
  110.      * Determines the current system architecture.
  111.      *
  112.      * @return The current system architecture.
  113.      */
  114.     public static Arch getArchitecture() {
  115.         String osArch = System.getProperty("os.arch");
  116.         Arch arch;
  117.  
  118.         if (osArch.equalsIgnoreCase("alpha")) {
  119.             arch = Arch.ALPHA;
  120.         }
  121.         else if (osArch.toLowerCase().startsWith("arm")) {
  122.             arch = Arch.ARM;
  123.         }
  124.         else if (osArch.equalsIgnoreCase("ia64_32")) {
  125.             arch = Arch.IA64_32;
  126.         }
  127.         else if (osArch.equalsIgnoreCase("ia64")) {
  128.             arch = Arch.IA64;
  129.         }
  130.         else if (osArch.equalsIgnoreCase("mips")) {
  131.             arch = Arch.MIPS;
  132.         }
  133.         else if (osArch.equalsIgnoreCase("sparc")) {
  134.             arch = Arch.SPARC;
  135.         }
  136.         else if (osArch.equalsIgnoreCase("sparc64")) {
  137.             arch = Arch.SPARC64;
  138.         }
  139.         else if (osArch.equalsIgnoreCase("ppc") ||
  140.                 osArch.equalsIgnoreCase("powerpc")) {
  141.             arch = Arch.PPC;
  142.         }
  143.         else if (osArch.equalsIgnoreCase("ppc64") ||
  144.                 osArch.equalsIgnoreCase("powerpc64")) {
  145.             arch = Arch.PPC64;
  146.         }
  147.         else if (osArch.equalsIgnoreCase("x86") ||
  148.             osArch.equalsIgnoreCase("i386") ||
  149.             osArch.equalsIgnoreCase("i486") ||
  150.             osArch.equalsIgnoreCase("i586") ||
  151.             osArch.equalsIgnoreCase("i686")) {
  152.             arch = Arch.x86;
  153.         }
  154.         else if (osArch.equalsIgnoreCase("x86_64") ||
  155.                 osArch.equalsIgnoreCase("amd64") ||
  156.                 osArch.equalsIgnoreCase("k8")) {
  157.             arch = Arch.x86_64;
  158.         }
  159.  
  160.         else {
  161.             arch = Arch.UNSUPPORTED;
  162.         }
  163.  
  164.         return arch;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement