Java System Properties

Java maintains a set of system properties for its operations. Each java system property is a key-value (String-String) pair. For example, one such system property is “java.version”=”1.7.0_09“.

Please note that access to system properties can be restricted by the Java security manager and policy file. By default, Java programs have unrestricted access to all the system properties.

We can retrieve all the system properties via System.getProperties() or we can also retrieve individual property via System.getProperty(key) method.

Table of Contents

1. Java System Properties List
2. Get System Property
3. Set System Property

1. Java System Properties List

  1. JRE System Properties

    java.homeJRE home directory, e.g., “C:\Program Files\Java\jdk1.7.0_09\jre“.
    java.library.pathJRE library search path for search native libraries. It is usually but not necessarily taken from the environment variable PATH.
    java.class.pathJRE classpath e.g., '.' (dot – used for current working directory).
    java.ext.dirsJRE extension library path(s), e.g, “C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext;C:\Windows\Sun\Java\lib\ext“.
    java.versionJDK version, e.g., 1.7.0_09.
    java.runtime.versionJRE version, e.g. 1.7.0_09-b05.
  2. IO System Properties

    file.separatorsymbol for file directory separator such as 'd:\test\test.java'. The default is '\' for windows or '/' for Unix/Mac.
    path.separatorsymbol for separating path entries, e.g., in PATH or CLASSPATH. The default is ';' for windows or ':' for Unix/Mac.
    line.separatorsymbol for end-of-line (or new line). The default is "\r\n" for windows or "\n" for Unix/Mac OS X.
  3. User Properties

    user.namethe user’s name.
    user.homethe user’s home directory.
    user.dirthe user’s current working directory.
  4. OS Properties

    os.namethe OS’s name, e.g., “Windows 7“.
    os.versionthe OS’s version, e.g., “6.1“.
    os.archthe OS’s architecture, e.g., “x86“.

2. Getting System Property

As discussed earlier, You can get the list of all the system properties via System.getProperties() or also retrieve individual property via System.getProperty(key).

Example 1: Java example to get the list of all system properties

import java.util.Properties;

public class PrintSystemProperties 
{
   public static void main(String[] a) 
   {
      // List all System properties
      Properties pros = System.getProperties();
      pros.list(System.out);
   }
}

Example 2: Java example to a system property value by its key

import java.util.Properties;
public class PrintSystemProperties 
{
   public static void main(String[] a) 
   {
      // List all System properties
      Properties pros = System.getProperties();
 
      // Get a particular System property given its key
      // Return the property value or null
      System.out.println(System.getProperty("java.home"));
      System.out.println(System.getProperty("java.library.path"));
      System.out.println(System.getProperty("java.ext.dirs"));
      System.out.println(System.getProperty("java.class.path"));
   }
}

3. Set System Property

In Java, you can set a custom system property either from the command line or from the application code itself.

Example 3: How to set system property in java using command line

In given example, the application will be able to access the property with key custom_key. It’s value will be available as custom_value.

java -Dcustom_key="custom_value" application_launcher_class

Example 4: How to set system property using Java code

Similar to above example, after executing this code, the application will be able to access the property with key custom_key. It’s value will be available as custom_value.

System.setProperty("custom_key", "custom_value");

That’s all for this basic tutorial for reading and writing system properties in java.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Java System Properties”

  1. Hey, good job, but!
    You didn’t mentioned a real world example in which cases I can use these existed or custom defined properties?

    Reply
  2. Informative and useful article. Java programmers are well known with java system properties and it is very important for the beginners to know about this. You have shared great post here, thanks for that.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.