Java System.setProperty 和 System.getProperty

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21204334/
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-13 07:18:46  来源:igfitidea点击:

System.setProperty and System.getProperty

java

提问by Rocky Hu

I didn't understand when I used System.setPropertyto define a parameter, where the data is stored?

以前System.setProperty定义参数的时候不明白,数据存放在哪里?

If say that I used System.setPropertyin one java file, then can I use the properties anywhere? But this is not true, I can't use it anywhere, only in the same java file I called setProperty.

如果说我System.setProperty在一个 java 文件中使用,那么我可以在任何地方使用这些属性吗?但这不是真的,我不能在任何地方使用它,只能在我调用的同一个 java 文件中使用它setProperty

I really do not know why and what the function of this method is.

我真的不知道这个方法的原因和功能是什么。

采纳答案by Keerthivasan

Systemclass has a staticmember variable named props which is of type Properties. Adding to that, Propertiesis a subtype of Hashtableclass. All the property values are stored as Key and Value. So, datastore is Hashtable.Answering the other question, You can very well use System.getProperty(propertyKey)method throughout your application since it is a public static method. You haven't understood how java programs work. When you run a Java program, you are actually starting a JVM instance. That instance will have its own System properties. That is where you have to put your property. When you run the other program, that will have its own System properties. So, you cannot expect a property which you set in one JVM instance to be accessible from another JVM instance!You can access the System.getProperty(propertyKey)in all classes running in the same JVM instance. Hope you can understand!

Systemclass 有一个static名为 props的成员变量,其类型为Properties。除此之外,PropertiesHashtable类的子类型。所有属性值都存储为 Key 和 Value。所以,datastore is Hashtable回答另一个问题,您可以System.getProperty(propertyKey)在整个应用程序中很好地使用方法,因为它是一个公共静态方法。你还没有理解java程序是如何工作的。当您运行 Java 程序时,您实际上是在启动一个 JVM 实例。该实例将拥有自己的系统属性。那就是你必须放置你的财产的地方。当您运行另一个程序时,它将拥有自己的系统属性。因此,您不能期望您在一个 JVM 实例中设置的属性可以从另一个 JVM 实例访问!您可以访问System.getProperty(propertyKey)在同一个 JVM 实例中运行的所有类中。希望你能理解!

回答by Pedro Rio

The data is stored in memory as long as your JVM instance is up. It it really isn't related to the file which called the methods.

只要您的 JVM 实例启动,数据就会存储在内存中。它确实与调用方法的文件无关。

How are you running your application? Is it a web application stored in an application sever? Can you post a sample of your code?

你如何运行你的应用程序?它是存储在应用程序服务器中的 Web 应用程序吗?您可以发布您的代码示例吗?

回答by Aniket Thakur

If you see source code of System Class it has following class variable

如果您看到 System Class 的源代码,它具有以下类变量

private static Properties props;

As for properties class you can think of it as a HashMap. It actually extends HashMap.

至于属性类,您可以将其视为 HashMap。它实际上扩展了 HashMap。

public class Properties extends Hashtable<Object,Object>

When you call

你打电话时

setProperty(String key, String value)

it actually does

它实际上是

props.setProperty(key, value);

This is just the summary(Security manager checks are also involved).

这只是摘要(还涉及安全管理器检查)。

Now why did I say it is per JVM instance?

现在为什么我说它是每个 JVM 实例?

When you start a Java process a separate JVM instance is created that runs your process. Also since props is a Class variable(not an instance variable) just one copy of it will be present in the corresponding Class instance which will be set when that class is loaded. Now this is under the assumption that you do not have any of your custom class loaders in which case behavior might be different. But for simplistic scenario you System.setProperty()and System.getProperty()will set system properties that you can access via any class running as a part of that java process(JVM).

当您启动 Java 进程时,会创建一个单独的 JVM 实例来运行您的进程。此外,由于 props 是一个类变量(不是实例变量),因此它的一个副本将存在于相应的类实例中,该实例将在加载该类时设置。现在这是假设您没有任何自定义类加载器,在这种情况下,行为可能会有所不同。但是,对于简单的情况下,你System.setProperty()System.getProperty()将设置系统属性,你可以通过任何类运行Access作为Java进程(JVM)的一部分。

回答by Arun

In the first Scenario, the moment A.java run, the JVM will stop and it will release all the values. And when the B.java run, the JVM will start fresh. So, the value will not persist across.

在第一个场景中,当 A.java 运行时,JVM 将停止并释放所有值。当 B.java 运行时,JVM 将重新启动。因此,该值不会持续存在。

In the second scenario, the JVM will not stop between the executions. So, the property value will persist from the second program.

在第二种情况下,JVM 不会在两次执行之间停止。因此,该属性值将从第二个程序中持续存在。