简体   繁体   中英

Java setting system property using command line

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.

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]

System properties (and other options like the classpath) need to be passed before program arguments. See the usage page :

java [ options ] class [ arguments ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM