开发者

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

开发者 https://www.devze.com 2023-02-28 04:44 出处:网络
I found only when I put -Dcontext=web into VM arguments, the value can be read by System.getproperty method. I a开发者_如何学Cm wondering what\'s the difference between those two?Program arguments are

I found only when I put -Dcontext=web into VM arguments, the value can be read by System.getproperty method. I a开发者_如何学Cm wondering what's the difference between those two?


Program arguments are arguments passed to your program and available in the args array of your main method

 public static void main(String[] args)

VM arguments are passed to the virtual machine and are designed to instruct the VM to do something. You can do things like control the heap size, etc. They can be accessed by your program via a call to System.getProperty() as you described.


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

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


Program arguments go into main() method:

public static void main(String[] args) // here


The Program args are available via the args [] of your main(String args[]) method


  • Program arguments - arguments which we normally pass into our program. This type parameters can be accessed using "args" String array in the main method.
  • VM arguments - arguments which are passed into the Java interpreter.
0

精彩评论

暂无评论...
验证码 换一张
取 消