What's the difference between program arguments and VM arguments?

前端 未结 5 1947
忘了有多久
忘了有多久 2021-01-31 03:15

I found only when I put -Dcontext=web into VM arguments, the value can be read by System.getproperty method. I am wondering what\'s the difference betw

5条回答
  •  粉色の甜心
    2021-01-31 03:33

    Program Argument: Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method.

    VM Argument: VM arguments are environment or system argument that needed by JVM to execute the program. VM arguments are read from system property as below java instruction.

    System.getProperty(sysProp1)

    Code Snippet:

    public static void main(String[] args) {
        String sysProp1 = "sysProp1";
        System.out.println("\t System Propery Name:" + sysProp1 + ", Value:" + System.getProperty(sysProp1));
        System.out.println("\t Program Variable Test Propery Name:" + args[0]);
    }
    

    There are Two way to pass these two params values.

    From Eclipse:

    As shown in the figure above

    Command Line Argument:

     java -cp -DsysProp1=testing123456 projectJar-2.0-SNAPSHOT-jar-with-dependencies.jar 123
    

    For a better presentation, in multiple lines

     java -cp 
          -DsysProp1=testing123456 
          projectJar-2.0-SNAPSHOT-jar-with-dependencies.jar 
          123
    

提交回复
热议问题