如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:54:24  来源:igfitidea点击:

How to add new System Properties in java

javapropertiessystem-properties

提问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.setPropertyor use the -Dname=valueflag 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));
}