Wednesday, April 28, 2010

Display System Properties using java

The System class contains several useful class fields and methods. It cannot be instantiated.

some of facilities provided by System class are standard input, standard output, error output streams,access to externally defined properties and environment variables etc

System Class provided method called getProperties() to get current system properties.

Properties getProperties()- Determines the current system properties.Please note that return types of this method is Properties. In order to display retirved properties, you need to iterate using example shown at below




Java Program to display System Properties :
package com.anuj.basic;

import java.util.*;

public class SystemProperties {

    public static void main(String[] args) {

        Properties p = System.getProperties();
        Set s = p.entrySet();
        Iterator i = s.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.println(me.getKey() + " - " + me.getValue());
            // Object o = i.next();
            // System.out.println(o);
        }

    }
}

Output :
java.runtime.name - Java(TM) SE Runtime Environment
sun.boot.library.path - C:\Program Files\Java\jre7\bin
java.vm.version - 22.1-b02
java.vm.vendor - Oracle Corporation
java.vendor.url - http://java.oracle.com/
path.separator - ;
java.vm.name - Java HotSpot(TM) Client VM
file.encoding.pkg - sun.io
user.country - US
user.script - 
sun.java.launcher - SUN_STANDARD
sun.os.patch.level - 
java.vm.specification.name - Java Virtual Machine Specification
user.dir - ######
java.runtime.version - 1.7.0_03-b05
java.awt.graphicsenv - sun.awt.Win32GraphicsEnvironment
java.endorsed.dirs - C:\Program Files\Java\jre7\lib\endorsed
os.arch - x86
java.io.tmpdir - ######
line.separator - 

java.vm.specification.vendor - Oracle Corporation
user.variant - 
os.name - Windows Vista
sun.jnu.encoding - Cp1252
java.library.path - ######
java.specification.name - Java Platform API Specification
java.class.version - 51.0
sun.management.compiler - HotSpot Client Compiler
os.version - 6.0
user.home - C:\Users\Anuj Patel
user.timezone - 
java.awt.printerjob - sun.awt.windows.WPrinterJob
file.encoding - Cp1252
java.specification.version - 1.7
java.class.path - D:\Eclipse\eclipse\myeclips\CoreJava\bin
user.name - ######
java.vm.specification.version - 1.7
sun.java.command - com.anuj.basic.SystemProperties
java.home - C:\Program Files\Java\jre7
sun.arch.data.model - 32
user.language - en
java.specification.vendor - Oracle Corporation
awt.toolkit - sun.awt.windows.WToolkit
java.vm.info - mixed mode, sharing
java.version - 1.7.0_03
java.ext.dirs - ######
sun.boot.class.path - ######
java.vendor - Oracle Corporation
file.separator - \
java.vendor.url.bug - http://bugreport.sun.com/bugreport/
sun.io.unicode.encoding - UnicodeLittle
sun.cpu.endian - little
sun.desktop - windows
sun.cpu.isalist - 


Author : Anuj Patel
Blog : http://goldenpackagebyanuj.blogspot.in/