简体   繁体   中英

default values in the HashTable extended by the Properties class

On a windows 2012 R2 platform I noticed that winver returns 6.3, but System.getProperty("os.version") returns 6.2 ; I am looking at this source code :

class  [More ...] Properties extends Hashtable<Object,Object> {

   protected Properties defaults;
   public String  [More ...] getProperty(String key) {
     Object oval = super.get(key);
     String sval = (oval instanceof String) ? (String)oval : null;
     return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
    }
}

I am suspecting the value of os.version is obtained from here. Is my suspicion right ?

Object oval = super.get(key);

What would be the contents of the HashTable and how is this populated ? ( I have not loaded the java source code as a project into my eclipse work-bench)

The system property os.version is added by the JVM itself thanks to the static native method initProperties(Properties props) as you can see here at the line 527 . This method is called while initializing the System class which is done by the method initializeSystemClass() .

In other words it means that the native code of your JVM is not able to recognize your os version, you should upgrade your JDK to fix this issue.

Here is a blog post where the blogger had the same issue with an old version of Java, upgrading it was enough to fix the issue.

This problem was seen before java 6u38. From 6u38, this issue is solved. The security baselines for the Java Runtime Environment (JRE) at the time of the release of JDK 6u38 are specified.

First using the EPM Java JDK version. 在此处输入图片说明

As you see it is generating incorrect information, now using a later JDK 7 release. 在此处输入图片说明

So this highlights it is the down to the version of Java which is causing the issue.

Resource Link:

  1. EPM 11.1.2.4 - Java versions and why Windows Server 2012 is not correctly recognised

I am suspecting the value of os.version is obtained from Object oval = super.get(key);. Is my suspicion right ?

Answer:

You are right. But here is some mechanism

First mechanism:

System.getProperty("os.version"); //which is called the OS version.

The getProperty method returns a string containing the value of the property. If the property does not exist, this version of getProperty returns null.

Second Mechanism:

System.getProperty("os.version", "Windows Server 2012 R2(6.3)");

getProperty requires two String arguments: the first argument is the key to look up and the second argument is a default value to return if the key cannot be found or if it has no value. For example, the following invocation of getProperty looks up the System property called os.version . This is not a valid system property, so instead of returning null, this method returns the default value provided as a second argument: "Windows Server 2012 R2(6.3)"

The last method provided by the System class to access property values is the getProperties method, which returns a Properties object. This object contains a complete set of system property definitions.


What would be the contents of the HashTable and how is this populated ?

Answer:

Properties extends java.util.Hashtable . Some of the methods inherited from Hashtable support the following actions:

  1. testing to see if a particular key or value is in the Properties object,
  2. getting the current number of key/value pairs,
  3. removing a key and its value,
  4. adding a key/value pair to the Properties list,
  5. enumerating over the values or the keys,
  6. retrieving a value by its key, and
  7. finding out if the Properties object is empty.

You can learn more from here about System Properties and Properties

Property related info is read and can be changed through this java class: PropertiesTest.java

Note and Recommendation from Oracle

Warning: Changing system properties is potentially dangerous and should be done with discretion. Many system properties are not reread after start-up and are there for informational purposes. Changing some properties may have unexpected side-effects.

Note: Some of the methods described above are defined in Hashtable, and thus accept key and value argument types other than String. Always use Strings for keys and values, even if the method allows other types. Also do not invoke Hashtable.set or Hastable.setAll on Properties objects; always use Properties.setProperty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM