System

Java System Properties Example

In this example we are going to demonstrate how to use Java System Properties. Java application can read System Properties, which provide information about the local system and configuration. When the java VM starts, its inserts local system properties into a system properties list. You can then use methods defined as part of the System Class to maintain the value of these properties.

1. Information of System Properties:

"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

You can use most of the above properties in your applications such as saving some temp or log files in the user directory or getting the user name to display it.

2. Writing System Properties:

To modify the existing set of system properties, use System.setProperties. This method takes a Properties object that has been initialized to contain the properties to be set. This method replaces the entire set of system properties with the new set represented by the Properties object.

3. Reading System Properties:

There are two System class methods used to read system properties: getProperty and getProperties.

3.1. getProperty:

Retrieve the value of the property named in the argument list, there are two different versions of getProperty.

  • Single argument version:

    Returns a string containing the value of the property based on the given property key but it will return null if the property does not exist. For example, to get the value of user.name, use the following statement:

    System.getProperty("user.name");
  • Two argument version:

    It requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value.
    For example, the following invocation of getProperty looks up the System property called fake.prop. This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: “another value here!”

    System.getProperty("fake.prop", "another value here!");

3.2. getProperties:

The System Class provide this method to access property values, which returns a Properties object. This object contains a complete set of system property definitions.

4. Example:

4.1. SystemProperties.java:

import java.io.FileInputStream;
import java.util.Properties;

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

        // creates a FileInputStream from file newProp.txt to load it into the new properties object 
        FileInputStream propFile =
            new FileInputStream("newProp.txt");

        // initializes p with the current set of system properties
        Properties p =
            new Properties(System.getProperties());
        
        // loads additional properties into p from the file newProp.txt
        p.load(propFile);

        // set the new system properties
        System.setProperties(p);

        // display the new system properties
        System.getProperties().list(System.out);
    }
}

4.2. Explanation:

  • In the above example, SystemProperties creates a Properties object which contains set of current system properties then add a new property from newProp.txt which contains:
    new.prop=new prop value here!
    user.name=JCG Author
  • The following statement initializes the new properties object p, with the current set of system properties, which in the case of this small application, is the set of properties initialized by the runtime system.
    Properties p = new Properties(System.getProperties());
  • Then the application loads additional properties into p from the file newProp.txt.
    p.load(propFile);
  • After that the application sets the system properties to p. This has the effect of adding the properties listed in newProp.txt to the set of properties created by the runtime system at startup.
    System.setProperties(p);

4.3. Notes:

  • An application can create p without any default Properties object, like this:
    Properties p = new Properties();
  • The value of system properties can be overwritten! For example, the newProp.txt contains the following line, the user.name system property will be overwritten:
    user.name=JCG Author

4.4. Output:

-- listing properties --
java.runtime.name=Java(TM) SE Runtime Environment
new.prop=new prop value here!
sun.boot.library.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/...
java.vm.version=20.1-b02
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
path.separator=:
java.vm.name=Java HotSpot(TM) Server VM
file.encoding.pkg=sun.io
user.country=US
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
user.dir=/home/ashraf/Desktop/JavaCodeGeek/Tut...
java.runtime.version=1.6.0_26-b03
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/...
os.arch=i386
java.io.tmpdir=/tmp
line.separator=

java.vm.specification.vendor=Sun Microsystems Inc.
os.name=Linux
sun.jnu.encoding=ANSI_X3.4-1968
java.library.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/...
java.specification.name=Java Platform API Specification
java.class.version=50.0
sun.management.compiler=HotSpot Tiered Compilers
os.version=2.6.32-21-generic
user.home=/home/ashraf
user.timezone=
java.awt.printerjob=sun.print.PSPrinterJob
file.encoding=ANSI_X3.4-1968
java.specification.version=1.6
user.name=JCG Author
java.class.path=.
java.vm.specification.version=1.0
sun.arch.data.model=32
java.home=/usr/lib/jvm/java-6-sun-1.6.0.26/jre
sun.java.command=SystemProperties
java.specification.vendor=Sun Microsystems Inc.
user.language=en
java.vm.info=mixed mode
java.version=1.6.0_26
java.ext.dirs=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/...
sun.boot.class.path=/usr/lib/jvm/java-6-sun-1.6.0.26/jre/...
java.vendor=Sun Microsystems Inc.
file.separator=/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=gnome
sun.cpu.isalist=

5. Download the Source Code of this example:

This was an example of how to use Java System Properties.

Download
You can download the full source code of this example here : JavaSystemPropertiesExampleCode

Ashraf Sarhan

Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button