I searched the internet for more than couple of hours and could not reach any conclusion.
Recently I decided to use BouncyCastle for SSL but I wanted it to off by default, so that BouncyCastle jar may not be in the class path.
private void enableBouncyCastleForSSL() {
if (config.isBouncyCastleEnabled()) {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
}
Even when config is disabled, it was looking for BouncyCastle and it failed with class loader error. java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
I tried moving just the line Security.insertProviderAt(new BouncyCastleProvider(), 1); to a new method it exhibited the same problem.
But when I introduce a class and move the BouncyCastle inside it, when the config is disabled, class loader issue does not appear
private void setupSSLProvider() {
if (voldemortConfig.isBouncyCastleEnabled()) {
SetupSSLProvider.useBouncyCastle();
}
}
public class SetupSSLProvider {
public static void useBouncyCastle() {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
}
}
Some articles claim that Class is loaded only when it is first used. http://www.programcreek.com/2013/01/when-and-how-a-java-class-is-loaded-and-initialized/
Apparently in my case, Java8 loads the class referenced in a class.
So my understanding is Java will load the classes one level deep, before executing first line of code in a class. Is that right ?