Header Ads

How To Get System Properties In Java

For each system , system properties information (persistent ) will be maintained in the form of system properties. Mainly we use properties to maintain system configuration.The system class will maintain a Properties object to get all the details.

In short it is nothing but a key value pair in the form of string.
The important aspect of System property is "to access any value from it , it requires current security manager's approval"

More details can be found here:

Lets see an example:
import java.util.Properties;
public class systemTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Properties p=System.getProperties();
  p.list(System.out);
  
 }
}

output:
java.vm.specification.vendor=XYZ Corporation
user.variant=
os.name=Windows XP
sun.jnu.encoding=Cp1252
java.library.path=C:\Program Files\Java\jdk1.7.0_21\bin...
java.specification.name=Java Platform API Specification
java.class.version=51.0
sun.management.compiler=HotSpot Client Compiler
os.version=5.1
user.home=C:\Documents and Settings\anichatt.AP...
user.timezone=
java.awt.printerjob=sun.awt.windows.WPrinterJob
file.encoding=Cp1252
java.specification.version=1.7
user.name=anichatt
java.class.path=E:\myProgram\
java.vm.specification.version=1.7
sun.arch.data.model=32
java.home=C:\Program Files\Java\jdk1.7.0_21\jre
sun.java.command=systemTest
java.specification.vendor=xyz Corporation
user.language=en
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode, sharing
java.version=1.7.0_21
java.ext.dirs=C:\Program Files\Java\jdk1.7.0_21\jre...
sun.boot.class.path=C:\Program Files\Java\jdk1.7.0_21\jre...
java.vendor=xyz Corporation
file.separator=\
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+m...

 Below are the system properties that can be tracked using system class's Property Object.


Key Meaning
"file.separator" Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
"java.class.path" Path used to find directories and JAR archives containing class files. Elements of the class path are separated by a platform-specific character specified in the path.separator property.
"java.home" Installation directory for Java Runtime Environment (JRE)
"java.vendor" JRE vendor name
"java.vendor.url" JRE vendor URL
"java.version" JRE version number
"line.separator" Sequence used by operating system to separate lines in text files
"os.arch" Operating system architecture
"os.name" Operating system name
"os.version" Operating system version
"path.separator" Path separator character used in java.class.path
"user.dir" User working directory
"user.home" User home directory
"user.name" User account name
taken from docs.oracle.com
How to get single property from the list??
say i want to retrieve OS name and current location of the class file.
so the corresponding code:
import java.util.Properties;
public class systemTest {
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Properties p=System.getProperties();
  //p.list(System.out);
  System.out.println(p.getProperty("java.class.path"));
  System.out.println(p.getProperty("os.name"));
  }

}

output:
. (dot -it means current working directory)
Windows XP

Powered by Blogger.