When does a Java class load?

I searched the Internet for more than two hours and could not come to any conclusion.

I recently decided to use BouncyCastle for SSL, but I wanted it to be disabled by default, so the BouncyCastle banner could not be in the class path.

private void enableBouncyCastleForSSL() {
   if (config.isBouncyCastleEnabled()) {
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
} 

Even when config is disabled, it searches for a BouncyCastle, and it does not cope with the class loader error. java.lang.NoClassDefFoundError: org / bouncycastle / jce / provider / BouncyCastleProvider

I tried to move only the line Security.insertProviderAt (new BouncyCastleProvider (), 1); to the new method, he discovered the same problem.

But when I introduce the class and move the BouncyCastle inside it when the configuration is disabled, the class loader problem does not appear

private void setupSSLProvider() {
    if (voldemortConfig.isBouncyCastleEnabled()) {
        SetupSSLProvider.useBouncyCastle();
    }
}
public class SetupSSLProvider {
  public static void useBouncyCastle() {
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
  }
}

, , . http://www.programcreek.com/2013/01/when-and-how-a-java-class-is-loaded-and-initialized/

-, Java8 , .

, , Java , . ?

+4
1

. , , . , " ", , , . , , . , NoClassDefFoundError.

:

  • (, )
  • ,
  • ,

BouncyCastleProvider , , new BouncyCastleProvider() Security.insertProviderAt(…), , Provider, .

, , , , :

Oracles JVM , , : , , , , , .

JVM, , JVM Reflection, , ( BouncyCastleProvider Class.forName("… .BouncyCastleProvider").newInstance() ).

+7

Source: https://habr.com/ru/post/1619775/


All Articles