What is the difference between java dotted arguments (e.g. -D) and no dash?

When we run the jar file on the command line

$ java -jar someJar.jar arg1 arg2

We can pass an argument by simply adding arguments with a space. But sometimes I come across arguments that start with dashes like -Darg1, -Darg2. What is the difference between the two?

+4
source share
2 answers

Without -Dyou create arguments that will be passed in mainto its array of strings. With the help of -Dyou define a system property available from System.getPropertiesand System.getProperty. Some system properties have predefined values, such as user.dirwhich defines the user's home directory. Read more about the "system properties" here .

java:

-Dproperty=value

. - , . value - , . , (, -Dfoo="foo bar").

+6

-D properties

: java -DsysProp1=value1 -DsysProp2=value2 -jar someJar.jar arg1 arg2

package test;

import java.io.IOException;

public class ArgsTest {

    public static void main(String[] args) throws IOException {

        System.out.println("Program Arguments:");
        for (String arg : args) {
            System.out.println("\t" + arg);
        }

        System.out.println("System Properties from VM Arguments");
        String sysProp1 = "sysProp1";
        System.out.println("\tName:" + sysProp1 + ", Value:" + System.getProperty(sysProp1));
        String sysProp2 = "sysProp2";
        System.out.println("\tName:" + sysProp2 + ", Value:" + System.getProperty(sysProp2));

    }
}
+3

Source: https://habr.com/ru/post/1688330/


All Articles