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 objectSystem.getProperty("key"); // returns the property specified by the key or nullSystem.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 keysfor(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.homejava.versionjava.vendorjava.vendor.urljava.vendor.url.bugjava.class.pathjava.library.pathjava.specification.namejava.specification.vendorjava.specification.versionjava.runtime.namejava.runtime.versionjava.class.versionjava.vm.namejava.vm.versionjava.vm.infojava.vm.vendorjava.vm.specification.namejava.vm.specification.vendorjava.vm.specification.versionjava.awt.graphicsenvjava.endorsed.dirsjava.io.tmpdirjava.awt.printerjobjava.ext.dirssun.java.launchersun.os.patch.levelsun.boot.library.pathsun.jnu.encodingsun.management.compilersun.arch.data.modelsun.io.unicode.encodingsun.cpu.endiansun.desktopsun.cpu.isalistsun.boot.class.pathuser.countryuser.diruser.variantuser.homeuser.timezoneuser.nameuser.languageos.archos.nameos.versionfile.encoding.pkgfile.encodingline.separatorpath.separatorfile.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().