程序参数和VM参数有什么区别?

问题描述:

我发现只有当我把-Dcontext=web放入VM参数时,该值可以通过System.getproperty方法读取。我想知道这两者之间有什么区别?程序参数和VM参数有什么区别?

程序参数的参数传递给您的程序和args阵列的主要方法可用

public static void main(String[] args) 

VM参数传递到虚拟机,其目的是指导虚拟机做些什么。您可以执行诸如控制堆大小等操作,您可以通过调用System.getProperty()来访问它们,如上所述。

+1

非常明确的答案:) – 2017-03-07 08:39:22

计划ARGS通过您的主要(字符串ARGS [])方法的

程序参数进入main()方法的ARGS []可用:

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

  • 程序参数 - 我们通常传入我们程序的参数。这个类型参数可以通过main方法中的“args”字符串数组访问。
  • VM参数 - 传递给Java解释器的参数。

[![enter image description here][1]][1]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 argument are environment or system argument that needed by JVM to execute program. VM arguments is 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: 

    [1]: https://i.stack.imgur.com/7c0dv.jpg 

Command Line Argument: 
java -cp projectJar-2.0-SNAPSHOT-jar-with-dependencies.jar com.first.jobs.IndexJob testing -DsysProp1=testing