Friday, September 11, 2015

Java find OS environment variables



//System.getProperty("Propertname") **Platform Independent** 
//System.getenv("EnvName")       **Platform Dependent**
 package Study;  
 import java.util.Collection;  
 import java.util.Iterator;  
 import java.util.Map;  
 import java.util.Set;  
 public class SystemVariables {  
      public static void main(String[] args) {  
           // TODO Auto-generated method stub  
           Variables var = new Variables();  
           var.getVariables();  
           var.getEnvVariables();  
      }  
 }  
 class Variables {  
      public void getVariables() {  
           Map<String, String> map = System.getenv();  
           // System.out.println(map.size());  
           Collection<String> allValues = map.values();  
           Set<String> key = map.keySet();  
           Iterator<String> keyIterator = key.iterator();  
           while (keyIterator.hasNext()) {  
                 Iterator<String> valueIterator = allValues.iterator();  
                 while (valueIterator.hasNext()) {  
                      System.out.print(keyIterator.next()+": ");  
                 System.out.println(valueIterator.next());  
                 }  
           }  
      }  
      public void getEnvVariables() {  
           String getEnv = System.getenv("SHELL");  
           // System.out.println(getEnv);  
      }  
 }  

No comments:

Post a Comment