Question about system properties and version strings

Magnus Ihse Bursie magnus.ihse.bursie at oracle.com
Wed Apr 15 12:37:24 UTC 2015


When this simple program is run:

public class TestSystemVersion {
   public static void main(String[] args) {
     System.out.println("1. java.version: " + 
System.getProperty("java.version"));
     System.out.println("1. java.runtime.version: " + 
System.getProperty("java.runtime.version"));
     System.setProperties(null);
     System.out.println("2. java.version: " + 
System.getProperty("java.version"));
     System.out.println("2. java.runtime.version: " + 
System.getProperty("java.runtime.version"));
   }
}

The output is:
1. java.version: 1.9.0-internal
1. java.runtime.version: 1.9.0-internal-magnusi_2015_04_02_09_24-b00
2. java.version: 1.9.0-internal
2. java.runtime.version: null

Is this really expected? I find it surprising. I'd have thought that 
either *both* strings would have been reset, or none.

This behavior stems from the fact that System.setProperties(), when 
given a null argument, calls initProperties. This is a native method 
which sets java.version, but not java.runtime.version. However, at 
system startup, initializeSystemClass() will be called, which apart from 
initProperties also calls a bunch of other methods to set up initial 
system properties, for instance sun.misc.Version.init(), which will 
re-set java.version (even though it is already set at this point), and 
also set java.runtime.version.

/Magnus



More information about the core-libs-dev mailing list