Java System Properties

Java maintains a Set of system properties that can be accessed in the runtime by executing programs. Each system property is a key-value pair. For example, one such system property is “java.version”=”1.7.0_09“.

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

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.

1. Java System Properties List

The following is a list of important system properties in each category.

1.1. Runtime Environment 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.

1.2. File 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.

1.3. User Properties

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

1.4. Operation System 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. Get the Value of a 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).

2.1. List of all System Properties

Properties pros = System.getProperties();

pros.list(System.out);

2.2. Get a system property value by its key

System.getProperty("java.home");

3. Set a System Property

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

3.1. Using Command Line

In the 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

3.2. Using Java Code

Similar to the 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 !!

Comments

Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.