Unexpected behavior in loading JVM class (ClassNotFoundException before class is really necessary)

I need help trying to understand why this is happening to me:

Using Java 1.8.0_131 , I have a class, for example:

public class DynamicClassLoadingAppKO {

    /*
     * THIS VERSION DOES NOT WORK, A ClassNotFoundException IS THROWN BEFORE EVEN EXECUTING main()
     */


    // If this method received ChildClassFromLibTwo, everything would work OK!
    private static void showMessage(final ParentClassFromLibOne obj) {
        System.out.println(obj.message());
    }


    public static void main(final String[] args) throws Throwable {

        try {

            final ChildClassFromLibTwo obj = new ChildClassFromLibTwo();
            showMessage(obj);

        } catch (final Throwable ignored) {
            // ignored, we just wanted to use it if it was present
        }

        System.out.println("This should be displayed, but no :(");

    }

}

Two other classes are used there: ParentClassFromLibOneand ChildClassFromLibTwo. The latter extends from the first.

There are two external libraries:

  • One library is called liboneand contains a class ParentClassFromLibOne. The application includes this library in the class path for both compilation and execution.
  • The second library is called libtwoand contains the class ChildClassFromLibTwo. The application includes this library in the classpath for compilation , but not for execution .

, Java ChildClassFromLibTwo ( ):

final ChildClassFromLibTwo obj = new ChildClassFromLibTwo();

, , ClassNotFoundException, try...catch (Throwable), System.out.println .

, ClassNotFoundException, DynamicClassLoadingAppKO, , main() , try...catch.

, , , , , showMessage(), :

/*
 * THIS VERSION WORKS OK, BECAUSE showMessage RECEIVES THE CHILD CLASS AS A PARAMETER
 */
private static void showMessage(final ChildClassFromLibTwo obj) {
    System.out.println(obj.message());
}

? , ?

GitHub, [1].

[1] https://github.com/danielfernandez/test-dynamic-class-loading/tree/20170504

+4
2

, , , Spring [1], , . .

-, , , , , showMessage() ParentClassFromLibOne. , ClassNotFoundException , ParentClassFromLibOne .

, , , main() showMessage(). , a ParentClassFromLibOne, : ChildClassFromLibTwo.

, ChildClassFromLibTwo, , ParentClassFromLibOne.

, , ParentClassFromLibOne , Object .

, , showMessage(...) ChildClassFromLibTwo , , , .

, , , , , ( , ClassNotFoundException).

[1] https://github.com/spring-projects/spring-boot/issues/8181

+4

, . , . , , -.

, -noverify Java, , , , . -noverify , , JVM.

+2

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


All Articles