Search code examples
javajvmenvironment-variablesjvm-hotspot

How are Java system properties derived from the host?


There is a set of system properties available by default in a JVM, as described here. Where in Hotspot are they derived?

Properties like user.home must be determined differently on different OSes, and I'm looking for the code that does this.


Solution

  • I know it's in OpenJDK but I haven't been able to find it yet

    A common approach is to check out the OpenJDK source tree from the mercurial repository and use some sort of a find command to look for occurrences of what you are looking for. In that case, a simple

    $ find . -type f -exec grep "user\.home" {} \; -print
    

    leads to ./jdk/src/windows/native/java/lang/java_props_md.c (and similar files for other operating systems, like ./jdk/src/solaris/native/java/lang/java_props_md.c) where there is a function

    java_props_t *
    GetJavaProperties(JNIEnv* env) { ... }
    

    In this function, the system property values are read through the operating system specific APIs.

    The function is called in Java_java_lang_System_initProperties(JNIEnv *env, jclass cla, jobject props) in the source file ./jdk/src/share/native/java/lang/System.c which again is declared as native method in ./jdk/src/share/classes/java/lang/System.java:

    private static native Properties initProperties(Properties props);
    

    The final initialization sequence is a bit tricky:

    • java.lang.System has a static initializer which calls the native registerNatives() method.
    • This native method calls java.lang.System.initializeSystemClass() (which is Java again).
    • initializeSystemClass() finally calls the above mentioned native initProperties() method to create and initialize the system properties.