While reading java man
page, I found the -Dproperty=value
flag which stats that by passing this flag, it will create a system property with value = value.
I wrote a test java code:
class File{
public static void main(String[] args){
System.out.println("HOLA");
System.out.println(System.getProperty("blah"));
}
}
I compiled the same with javac File.java
and then ran with with command java File -Dblah=blah
but I got the following output
HOLA
null
and then I ran with as java -Dblah=blah File
and then I got the expected output:
HOLA
blah
The question is: Is this a bug or is this an intentional behavior. It does seem a bug because in most of the program, order doesn't matter at command line.
Set System Property. In java, you can set a custom system property either from command tools or from java code itself. Set system property from code using System.setProperty() methodSystem.setProperty("custom_key", "custom_value"); That’s all for this basic tutorial for reading and writing system properties in java.
Setting the Value of a System Property from the Command Line: add -D option to the java command when running your program.
We can set System Properties with maven in several ways, either using maven plugin or from command-line or from a file. 1. From Command line To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D {systemproperty}= {propertyvalue} parameter in commandline.
How to set environment variables for Java using command line 1 JAVA_HOME: stores location of the JDK’s installation directory. When you install development tools, they will first... 2 PATH: stores paths of directories where the operating system will look, to launch the requested programs quickly. For... More ...
The -D
needs to come before the class name, because anything after the class name is regarded as an argument to the Java app itself... and the Java app may choose to do anything it likes with -D
, or any other JVM options.
Basically, the syntax is:
java [jvm-args] class-name [application-args]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With