Java determines the operating system type (applicable to various operating systems)

Source: Internet
Author: User

For windows systems, the version is not determined here. If necessary, you can continue to judge the version after determining windows. The java version needs to be considered. The version is different and the results are different. The code below: 1. enumeration type: EPlatform [java]/*** platform * @ author isea533 */public enum EPlatform {Any ("any"), Linux ("Linux "), mac_ OS ("Mac OS"), Mac_ OS _X ("Mac OS x"), Windows ("Windows"), OS2 ("OS/2"), Solaris ("Solaris "), sunOS ("SunOS"), MPEiX ("MPE/iX"), HP_UX ("HP-UX"), AIX ("AIX"), OS390 ("OS/390 "), freeBSD ("FreeBSD"), Irix ("Irix"), Digital_Unix ("Digital Unix"), NetWare_411 ("NetWare"), OSF1 ("OSF1"), OpenVMS ("OpenV MS "), Others (" Others "); private EPlatform (String desc) {this. description = desc;} public String toString () {return description;} private String description;} 2. operating System class: OSinfo [java]/*** operating System class: * Get System. getProperty ("OS. name ") Corresponding OS * @ author isea533 */public class OSinfo {private static String OS = System. getProperty ("OS. name "). toLowerCase (); private static OSinfo _ instance = new OSinfo (); p Rivate EPlatform platform; private OSinfo () {} public static boolean isLinux () {return OS. indexOf ("linux")> = 0;} public static boolean isMacOS () {return OS. indexOf ("mac")> = 0 & OS. indexOf ("OS")> 0 & OS. indexOf ("x") <0;} public static boolean isMacOSX () {return OS. indexOf ("mac")> = 0 & OS. indexOf ("OS")> 0 & OS. indexOf ("x")> 0;} public static boolean isWindows () {return OS. indexOf ("windows")> = 0;} public stat Ic boolean isOS2 () {return OS. indexOf ("OS/2")> = 0;} public static boolean isSolaris () {return OS. indexOf ("solaris")> = 0;} public static boolean isSunOS () {return OS. indexOf ("sunos")> = 0;} public static boolean isMPEiX () {return OS. indexOf ("mpe/ix")> = 0;} public static boolean isHPUX () {return OS. indexOf ("hp-ux")> = 0;} public static boolean isAix () {return OS. indexOf ("aix")> = 0;} public static bool Ean isOS390 () {return OS. indexOf ("OS/390")> = 0;} public static boolean isFreeBSD () {return OS. indexOf ("freebsd")> = 0;} public static boolean isIrix () {return OS. indexOf ("irix")> = 0;} public static boolean isDigitalUnix () {return OS. indexOf ("digital")> = 0 & OS. indexOf ("unix")> 0;} public static boolean isNetWare () {return OS. indexOf ("netware")> = 0;} public static boolean isOSF1 () {return OS. indexOf (" Osf1 ")> = 0;} public static boolean isOpenVMS () {return OS. indexOf ("openvms")> = 0;}/*** get the operating system name * @ return operating system name */public static EPlatform getOSname () {if (isAix ()) {_ instance. platform = EPlatform. AIX;} else if (isDigitalUnix () {_ instance. platform = EPlatform. digital_Unix;} else if (isFreeBSD () {_ instance. platform = EPlatform. freeBSD;} else if (isHPUX () {_ instance. platform = EPlatform. HP_UX ;} Else if (isIrix () {_ instance. platform = EPlatform. irix;} else if (isLinux () {_ instance. platform = EPlatform. linux;} else if (isMacOS () {_ instance. platform = EPlatform. mac_ OS;} else if (isMacOSX () {_ instance. platform = EPlatform. mac_ OS _X;} else if (isMPEiX () {_ instance. platform = EPlatform. MPEiX;} else if (isNetWare () {_ instance. platform = EPlatform. netWare_411;} else if (isOpenVMS ()) {_ Instance. platform = EPlatform. openVMS;} else if (isOS2 () {_ instance. platform = EPlatform. OS2;} else if (isOS390 () {_ instance. platform = EPlatform. OS390;} else if (isOSF1 () {_ instance. platform = EPlatform. OSF1;} else if (isSolaris () {_ instance. platform = EPlatform. solaris;} else if (isSunOS () {_ instance. platform = EPlatform. sunOS;} else if (isWindows () {_ instance. platform = EPlatfo Rm. windows;} else {_ instance. platform = EPlatform. others;} return _ instance. platform;}/*** @ param args */public static void main (String [] args) {System. out. println (OSinfo. getOSname () ;}} I used Windows 7 to identify it: Windows. If you use another operating system, I hope you can leave a message here for the operating system and result. If the result is incorrect, you can use the following code to obtain your operating System information: [java] class WhatOS {public static void main (String args []) {System. out. println (System. getProperty ("OS. name "); System. out. println (System. getProperty ("OS. version "); System. out. println (System. getProperty ("OS. arch "));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.