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.home | JRE home directory, e.g., “C:\Program Files\Java\jdk1.7.0_09\jre “. |
java.library.path | JRE library search path for search native libraries. It is usually but not necessarily taken from the environment variable PATH. |
java.class.path | JRE classpath e.g., '.' (dot – used for current working directory). |
java.ext.dirs | JRE extension library path(s), e.g, “C:\Program Files\Java\jdk1.7.0_09\jre\lib\ext;C:\Windows\Sun\Java\lib\ext “. |
java.version | JDK version, e.g., 1.7.0_09 . |
java.runtime.version | JRE version, e.g. 1.7.0_09-b05 . |
1.2. File System Properties
file.separator | symbol for file directory separator such as 'd:\test\test.java' . The default is '\' for windows or '/' for Unix/Mac. |
path.separator | symbol for separating path entries, e.g., in PATH or CLASSPATH . The default is ';' for windows or ':' for Unix/Mac. |
line.separator | symbol 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.name | the user’s name. |
user.home | the user’s home directory. |
user.dir | the user’s current working directory. |
1.4. Operation System Properties
os.name | the OS’s name, e.g., “Windows 7 “. |
os.version | the OS’s version, e.g., “6.1 “. |
os.arch | the 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