Java to determine the operating system type (for various operating systems)

Source: Internet
Author: User

In the recent period of time to write an authorized program, need to obtain a lot of information to ensure that the program can not be copied to use, must be authorized.

In order to limit the operating system that users use, there must be a uniform way to obtain it.

In Java, through System.getproperty ("Os.name") to obtain, through the reference:http://lopica.sourceforge.net/os.html to achieve the judgment of each operating system.

For Windows, this is not the specific version, if necessary, you can judge the windows, continue to judge, the decision needs to consider the Java version, the version is different, the results are not the same.

The following code:

1. Enumeration type: Eplatform

/** * Platform * @author isea533 */public enum Eplatform {any ("any"), Linux ("Linux"), Mac_os ("Mac OS"), Mac_os_x ("Mac OS X"), window S ("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 ("OpenVMS"), Others ("Others");p rivate eplatform (String desc) {this.description = desc;} Public String toString () {return description;} Private String description;}

2. Operating System class: OSinfo

/** *  operating System class: *  get System.getproperty ("Os.name") corresponding operating system  *  @author  isea533  */public class OSinfo {private static String OS =  System.getproperty ("Os.name"). toLowerCase ();p rivate 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 static 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 boolean 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 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 = eplatform.windows;} Else{_instance.platform = eplatform.others;} Return _instance.platform;} /** *  @param  args */public static void main (String[] args)  { System.out.println (Osinfo.getosname ());}}




If the result is wrong, you can use the following code to get your operating system information:

Class Whatos {public static void main (String args[]) {System.out.println (System.getproperty ("Os.name"));//This method    You can output the operating system name directly.    System.out.println (System.getproperty ("os.version"));  System.out.println (System.getproperty ("Os.arch")); }}

Original posts: http://blog.csdn.net/isea533/article/details/8449919

Java to determine the operating system type (for various operating systems)

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.