官方解析
Set a system property value. If value is a string that contains spaces, you must enclose the string in double quotes:
java -Dfoo="some string" SomeClass
-D参数是用来设置一个系统属性,如果该值是一个字符串且包含空格,那么这个值需要包含到一对双引号中。
实例
代码如下,启动时加上JVM参数-Dcom.wenqi="12 空格 34"
public class TestJVMOptions {
public static void main(String[] args) {
System.out.println(System.getProperty("com.wenqi"));
}
}
输出
12 空格 34
经过简单的Demo我们就可以知道启动时加上-D的作用了。我们可以通过-D来设置系统的属性,代码中通过System.getProperty
方法就可以获取到这个属性,执行相应的业务代码了,这就很灵活方便了。