我发现只有当我-Dcontext=web输入VM参数时,才能通过System.getproperty方法读取该值.我想知道这两者之间有什么区别?
Chr*_*son 31
程序参数是传递给程序的参数,可在argsmain方法的数组中使用
public static void main(String[] args)
Run Code Online (Sandbox Code Playgroud)
VM参数传递给虚拟机,旨在指示VM执行某些操作.您可以执行诸如控制堆大小等操作.程序可以通过System.getProperty()您所描述的调用来访问它们.
小智 5
程序参数:程序参数是传递给应用程序的参数,可通过主方法的“args”字符串数组参数访问。
VM 参数: VM 参数是 JVM 执行程序所需的环境或系统参数。VM 参数是从系统属性中读取的,如下面的 java 指令。
System.getProperty(sysProp1)
代码片段:
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]);
}
Run Code Online (Sandbox Code Playgroud)
有两种方法可以传递这两个参数值。
从 Eclipse:
如上图所示
命令行参数:
java -cp -DsysProp1=testing123456 projectJar-2.0-SNAPSHOT-jar-with-dependencies.jar 123
Run Code Online (Sandbox Code Playgroud)
为了更好的演示,多行
java -cp
-DsysProp1=testing123456
projectJar-2.0-SNAPSHOT-jar-with-dependencies.jar
123
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10819 次 |
| 最近记录: |