Wednesday, October 23, 2013

What's an easy way to access System Property information?
Author: Deron Eriksson
Description: This Java tutorial describes how to use SystemUtils from Commons Lang to conveniently access System Property information.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The SystemUtils class in the Commons LangS library provides a large set of public static fields which allow for convenient access to System Property values. This comes in very handy if your IDE features code completion, like EclipseSW, shown below.
Using SystemUtils with Eclipse Content Assist
In general, it can be fairly difficult to remember the particular name of a System property (to plug into System.getProperty()). However, SystemUtils plus code completion makes it unnecessary to memorize a bunch of System property names.
The UserNameTest class is a simple example of using SystemUtils to get the current user name.

UserNameTest.java

package test;

import org.apache.commons.lang.SystemUtils;

public class UserNameTest {

	public static void main(String args[]) throws Exception {

		System.out.print("User name from SystemUtils: ");
		System.out.println(SystemUtils.USER_NAME);
	}
}
The output of UserNameTest is shown here.

Results

User name from SystemUtils: Deron


Let's look at all the public fields in SystemUtils and their values. To do this, I wrote the SystemUtilsTest class.

SystemUtilsTest.java

package test;

import java.lang.reflect.Field;

import org.apache.commons.lang.SystemUtils;

public class SystemUtilsTest {

	public static void main(String args[]) throws Exception {
		Class c = SystemUtils.class;
		Field[] fields = c.getFields();
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			System.out.printf("%-40s :" + f.get(c) + "\n", c.getSimpleName() + "." + f.getName());
		}
	}
}
The output of SystemUtilsTest is shown here.

Results

SystemUtils.AWT_TOOLKIT                  :sun.awt.windows.WToolkit
SystemUtils.FILE_ENCODING                :Cp1252
SystemUtils.FILE_SEPARATOR               :\
SystemUtils.JAVA_AWT_FONTS               :null
SystemUtils.JAVA_AWT_GRAPHICSENV         :sun.awt.Win32GraphicsEnvironment
SystemUtils.JAVA_AWT_HEADLESS            :null
SystemUtils.JAVA_AWT_PRINTERJOB          :sun.awt.windows.WPrinterJob
SystemUtils.JAVA_CLASS_PATH              :C:\projects\workspace\testing\bin;C:\projects\workspace\testing\lib\commons-io-1.4.jar;C:\projects\workspace\testing\lib\commons-lang-2.3.jar
SystemUtils.JAVA_CLASS_VERSION           :49.0
SystemUtils.JAVA_COMPILER                :null
SystemUtils.JAVA_ENDORSED_DIRS           :C:\Program Files\Java\jre1.5.0_09\lib\endorsed
SystemUtils.JAVA_EXT_DIRS                :C:\Program Files\Java\jre1.5.0_09\lib\ext
SystemUtils.JAVA_HOME                    :C:\Program Files\Java\jre1.5.0_09
SystemUtils.JAVA_IO_TMPDIR               :C:\DOCUME~1\Deron\LOCALS~1\Temp\
SystemUtils.JAVA_LIBRARY_PATH            :C:\Program Files\Java\jre1.5.0_09\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\jdk1.5.0_09\bin\..\jre\bin\client;C:\jdk1.5.0_09\bin\..\jre\bin;C:\jdk1.5.0_09\bin;C:\maven-2.0.8\bin;C:\Program Files\Mail Enable\BIN;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\jadnt158;C:\mysql-essential-5.0.27\bin;C:\Program Files\QuickTime\QTSystem\;C:\putty;C:\CVSNT\
SystemUtils.JAVA_RUNTIME_NAME            :Java(TM) 2 Runtime Environment, Standard Edition
SystemUtils.JAVA_RUNTIME_VERSION         :1.5.0_09-b03
SystemUtils.JAVA_SPECIFICATION_NAME      :Java Platform API Specification
SystemUtils.JAVA_SPECIFICATION_VENDOR    :Sun Microsystems Inc.
SystemUtils.JAVA_SPECIFICATION_VERSION   :1.5
SystemUtils.JAVA_UTIL_PREFS_PREFERENCES_FACTORY :null
SystemUtils.JAVA_VENDOR                  :Sun Microsystems Inc.
SystemUtils.JAVA_VENDOR_URL              :http://java.sun.com/
SystemUtils.JAVA_VERSION                 :1.5.0_09
SystemUtils.JAVA_VM_INFO                 :mixed mode
SystemUtils.JAVA_VM_NAME                 :Java HotSpot(TM) Client VM
SystemUtils.JAVA_VM_SPECIFICATION_NAME   :Java Virtual Machine Specification
SystemUtils.JAVA_VM_SPECIFICATION_VENDOR :Sun Microsystems Inc.
SystemUtils.JAVA_VM_SPECIFICATION_VERSION :1.0
SystemUtils.JAVA_VM_VENDOR               :Sun Microsystems Inc.
SystemUtils.JAVA_VM_VERSION              :1.5.0_09-b03
SystemUtils.LINE_SEPARATOR               :

SystemUtils.OS_ARCH                      :x86
SystemUtils.OS_NAME                      :Windows XP
SystemUtils.OS_VERSION                   :5.1
SystemUtils.PATH_SEPARATOR               :;
SystemUtils.USER_COUNTRY                 :US
SystemUtils.USER_DIR                     :C:\projects\workspace\testing
SystemUtils.USER_HOME                    :C:\Documents and Settings\Deron
SystemUtils.USER_LANGUAGE                :en
SystemUtils.USER_NAME                    :Deron
SystemUtils.USER_TIMEZONE                :
SystemUtils.JAVA_VERSION_TRIMMED         :1.5.0_09
SystemUtils.JAVA_VERSION_FLOAT           :1.5
SystemUtils.JAVA_VERSION_INT             :150
SystemUtils.IS_JAVA_1_1                  :false
SystemUtils.IS_JAVA_1_2                  :false
SystemUtils.IS_JAVA_1_3                  :false
SystemUtils.IS_JAVA_1_4                  :false
SystemUtils.IS_JAVA_1_5                  :true
SystemUtils.IS_JAVA_1_6                  :false
SystemUtils.IS_OS_AIX                    :false
SystemUtils.IS_OS_HP_UX                  :false
SystemUtils.IS_OS_IRIX                   :false
SystemUtils.IS_OS_LINUX                  :false
SystemUtils.IS_OS_MAC                    :false
SystemUtils.IS_OS_MAC_OSX                :false
SystemUtils.IS_OS_OS2                    :false
SystemUtils.IS_OS_SOLARIS                :false
SystemUtils.IS_OS_SUN_OS                 :false
SystemUtils.IS_OS_UNIX                   :false
SystemUtils.IS_OS_WINDOWS                :true
SystemUtils.IS_OS_WINDOWS_2000           :false
SystemUtils.IS_OS_WINDOWS_95             :false
SystemUtils.IS_OS_WINDOWS_98             :false
SystemUtils.IS_OS_WINDOWS_ME             :false
SystemUtils.IS_OS_WINDOWS_NT             :false
SystemUtils.IS_OS_WINDOWS_XP             :true

Among other things, from the results, you can see that I'm using JavaSW 1.5 and Windows XP.

No comments: