Search code examples
javacommand-linejvm-argumentssystem-properties

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.


Solution

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

    java [ options ] class [ arguments ]