Week 138 — What is a system property and how can it be passed to a Java application?

Question of the Week #138
What is a system property and how can it be passed to a Java application?
3 Replies
Eric McIntyre
Eric McIntyre3w ago
System properties are key-value pairs that can be set via the command-line when starting a Java application and can be accessed by that application at any time. They can be passed by adding -Dkey=value argument. The Java application can then read the system property using System.getProperty("key"). For example, consider the following program (SystemProperties.java):
public class SystemProperties {
public static void main(String[] args) {
String propertyValue = System.getProperty("exampleKey");
System.out.println("property value: " + propertyValue);
}
}
public class SystemProperties {
public static void main(String[] args) {
String propertyValue = System.getProperty("exampleKey");
System.out.println("property value: " + propertyValue);
}
}
When running that program using java -DexampleKey=someValue SystemProperties.java, the JVM creates a system property named exampleKey with the value someValue which can be accessed by the program resulting in it printing property value: someValue. In addition to reading system properties with System.getProperty, one can also modify them with the System.setProperty method
String key = "mykey";
String oldValue = System.getProperty(key);
System.setProperty(key, "new value");

System.out.println("old value: " + oldValue);
System.out.println("new value: " + System.getProperty(key));
String key = "mykey";
String oldValue = System.getProperty(key);
System.setProperty(key, "new value");

System.out.println("old value: " + oldValue);
System.out.println("new value: " + System.getProperty(key));
Furthermore, Java allows getting all current system properties using the System.getProperties() method. This method returns a Properties object containing the available properties:
Properties props = System.getProperties();
System.out.println(props.getProperty("java.home"));//prints the Java installation directory used by the current program

System.out.println("all properties:");
for (String key : props.stringPropertyNames()) {
System.out.println(key + " = " + props.getProperty(key));
}
Properties props = System.getProperties();
System.out.println(props.getProperty("java.home"));//prints the Java installation directory used by the current program

System.out.println("all properties:");
for (String key : props.stringPropertyNames()) {
System.out.println(key + " = " + props.getProperty(key));
}
As shown with java.home, Java sets many system properties by default. These provide information about the system (e.g. operating system, machine and user information) and information about the running JVM (e.g. Java version).
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre3w ago
Systems property provide a way for you to access and modify configuration settings and environmental detalis during program execution
Submission from silliersquash80
Eric McIntyre
Eric McIntyre3w ago
A system property is similar to an environment variable. It is a named value that is available to the entire application using the java.lang.System.getProperty or getProperties methods. The primary way to set system properties is via the -D command line arg to java, for example:
java -Dmy.sys.prop="Some arbitrary value" -Dother.prop=42
java -Dmy.sys.prop="Some arbitrary value" -Dother.prop=42
System properties can also be set within a running program, using the java.lang.System.setProperty and setProperties methods. This can be used, for instance, to set the properties from a file. Care must be taken here to ensure that the properties are initialized before they are needed by the rest of the program. Also note that the JVM defines its own set of properties that are present in the system, such as information about the OS and platform. Changing these properties (via command line or programmatically), may not have any effect, or may result in undefined behavior.
⭐ Submission from dangerously_casual

Did you find this page helpful?