Social Icons

Thursday, April 29, 2010

How to retrieve System properties in Java


This post contains the procedure to retrieve system data without using any external libraries. I use java.lang.System to retrieve data and java.util.Properties class to store them so that we can Iterate and retrieve system data in our code.
As you may know System class is a final class and cannot be instantiated , hence it provides us three static methods to retrieve system properties. Before using the second and third methods we need to know what these keys are.

System.getProperties(); // returns a Properties object
System.getProperty("key"); // returns the property specified by the key or null
System.getProperty("key", "default"); // returns the default val if key val is null


Apparently the first method returns a Properties object which essentially a Hashtable , because Properties class extends Hashtable. The Property object returned by the getProperties() has Object Keys and String values.hence we can retrieve keys of the Properties object and iterate through all the available keys in the Properties object. (Notice that getProperties method may throw a Security Exception in case the Security manger does not allow access to the System properties.)



import java.util.Properties;
import java.util.Iterator;
public class OS {
    public static void main(String[] a){
          
          String s="";
          try{
              Properties sys = System.getProperties();
              // Properties object is a hashtable
              // hence we need it's key to get it's properties
              // following for loop retirieve the keys 
              for(Iterator<Object> i= sys.keySet().iterator(); i.hasNext(); ){
                    s+= i.next().toString()+"\n";
              }
              System.out.println(s);
          }
          catch(SecurityException se){
              System.out.print(se.getMessage());
          }
    }
}


Here are the Keys available in the Property object sys. I ordered them by  categories. In order to sort 
it according to the natural order Collections.sort() method can be used on the keyset of the Properties object


java.home
java.version
java.vendor
java.vendor.url
java.vendor.url.bug
java.class.path
java.library.path
java.specification.name
java.specification.vendor
java.specification.version
java.runtime.name
java.runtime.version
java.class.version
java.vm.name
java.vm.version
java.vm.info
java.vm.vendor
java.vm.specification.name
java.vm.specification.vendor
java.vm.specification.version
java.awt.graphicsenv
java.endorsed.dirs
java.io.tmpdir
java.awt.printerjob
java.ext.dirs
sun.java.launcher
sun.os.patch.level
sun.boot.library.path
sun.jnu.encoding
sun.management.compiler
sun.arch.data.model
sun.io.unicode.encoding
sun.cpu.endian
sun.desktop
sun.cpu.isalist
sun.boot.class.path
user.country
user.dir
user.variant
user.home
user.timezone
user.name
user.language
os.arch
os.name
os.version
file.encoding.pkg
file.encoding
line.separator
path.separator
file.separator





Now we know the keys we can get properties we want by calling getProperties(String key).Here is a little tool
I made to get all available info using System.getProperties().


 
 
Blogger Templates http://slots.to/