Close

Java - System.getProperty() Examples

Java Java API 


Class:

java.lang.System

java.lang.Objectjava.lang.Objectjava.lang.Systemjava.lang.SystemLogicBig

Methods:

public static String getProperty(String key)

Returns the Java system property indicated by the specified key.



public static String getProperty(String key,
                                 String def)

Gets the system property indicated by the specified key. If there's no property with that key, the specified default value (def) is returned.


The difference between system properties and environment variables is : properties are set for the currently running Java process, they are usually set as -Dname=value while running a Java program. Whereas, Environmental variables are set on O.S. level e.g. path variable, JAVA_HOME etc.


Examples


package com.logicbig.example.system;

public class GetPropertyExample {

public static void main(String... args) {
String s = System.getProperty("java.io.tmpdir");
System.out.println(s);

String s2 = System.getProperty("myPropKey");
System.out.println(s2);

String s3 = System.getProperty("myPropKey",
"myPropDefaultValue");
System.out.println(s3);
}
}

Output

C:\Users\Joe\AppData\Local\Temp\
null
myPropDefaultValue




See Also