如何在java中添加新的系统属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9736862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add new System Properties in java
提问by Nelo Angelo
Is it possible to add new values to Java System Properties. If there is any how can introduce new keys with there corresponding values in Java System Properties.
是否可以向 Java 系统属性添加新值。如果有任何如何在 Java System Properties 中引入具有相应值的新键。
采纳答案by dty
Either System.setProperty
or use the -Dname=value
flag when you start the JVM
或者在启动 JVM 时System.setProperty
使用该-Dname=value
标志
回答by Chandra Sekhar
System.setProperties(properties object);
This will set the system properties.
这将设置系统属性。
If you want to set a specified property, then use
如果要设置指定的属性,则使用
System.setProperty(key, value);//Both key and value should be string.
NOTE: This will first check the permission and then set it. If permission denied, then SecurityException may occur.
注意:这将首先检查权限,然后设置它。如果权限被拒绝,则可能发生 SecurityException。
回答by assylias
Yes:
是的:
public static void main(String args[]) {
String key = "a new property";
System.setProperty(key, "a property with a value");
System.out.println(System.getProperty(key));
}