When we try to warm up our Java binary by invoking Class.forName to pre-load all the classes we use, okhttp3.internal.platform.Jdk9Platform throws an ExceptionInInitializerError exception.
The root cause is that there is a cyclic initialization between Jdk9Platform and its parent Platform:
Initialize Jdk9Platform > Initialize Platform > Initialize Platform.Companion > eventually calls Jdk9Platform.buildIfSupported() > at this point, the companion object of Jdk9Platform has not been initialized yet.
One possible way to solve this is to make Platform.platform lazy (does not call getPlatform() immediately).
Test:
import org.junit.Test;
public class OkHttpBug {
@Test
public void testFailure() throws Exception {
Class.forName("okhttp3.internal.platform.Jdk9Platform");
// Expected Behavior:
// The class okhttp3.internal.platform.Jdk9Platform should be loaded successfully without
// throwing any exceptions.
//
// Actual Failure:
// java.lang.ExceptionInInitializerError
// ...
// Caused by: java.lang.NullPointerException: Cannot invoke
// "okhttp3.internal.platform.Jdk9Platform$Companion.buildIfSupported()" because
// "okhttp3.internal.platform.Jdk9Platform.Companion" is null
System.out.println("Loaded Jdk9Platform successfully.");
}
}
When we try to warm up our Java binary by invoking
Class.forNameto pre-load all the classes we use,okhttp3.internal.platform.Jdk9Platformthrows anExceptionInInitializerErrorexception.The root cause is that there is a cyclic initialization between
Jdk9Platformand its parentPlatform:Initialize
Jdk9Platform> InitializePlatform> InitializePlatform.Companion> eventually callsJdk9Platform.buildIfSupported()> at this point, the companion object ofJdk9Platformhas not been initialized yet.One possible way to solve this is to make
Platform.platformlazy (does not callgetPlatform()immediately).Test: