What happens when I declare a custom system property in java?

129 views Asked by At

Wondering what happens when I declare a custom property in java using -D command. I came to know that other system properties are not stored, instead generated by JVM. So what will happen to the property that I created? Can I use it next time while I compile the code without declaring again?

example: java -D"custom_key"="custom_value" some_class

1

There are 1 answers

6
vanje On

System properties are evaluated at runtime only not at compilation time.

public class SysProp {
  public static void main(String[] args) {
    System.out.println(System.getProperty("foo", "<foo not set>"));
  }
}

java SysProp

Output: <foo not set>

java -Dfoo=bar

Output: bar