Saturday, June 15, 2013


How to identify your Operating System through your Java code?

How do you identify in which platform or operating system that your java application run on? below line would do it:

  String _OS = System.getProperty("os.name").toLowerCase(); 

This will represent the name of your operating system such as "windows","unix","mac", etc.
So you can use the "_OS" string to get the correct platform.
                                                                  
 if(_OS.indexOf("win") >= 0){                                                   
       System.out.println("This is Windows");                                        
 }else if (_OS.indexOf("mac") >= 0) {                                            
       System.out.println("This is Mac");                                            
 }else if(_OS.indexOf("nix")>=0||_OS.indexOf("nux")>=0||_OS.indexOf("aix")> 0){
       System.out.println("This is Unix or Linux");                                  
 } else if (_OS.indexOf("sunos") >= 0) {                                          
       System.out.println("This is Solaris");                                        
 } else {                                                                         
       System.out.println("Your OS is not support!!");                               
 }


That's all folks ! hope this small trick will save your time .



No comments: