Java HotSpot: lazy loading of classes is not working

Viewed 102
java version "1.8.0_202"
Java(TM) SE Runtime Environment (build 1.8.0_202-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.202-b08, mixed mode)

Java lazy loading of classes not working. e.g.,

public class Foo() {

   public Bar createBar() {
        return new Bar(false);  <----------raised exception
   }
}

public class Bar {
   public Bar(boolean createHello) {
      if (createHello) {
          Hello hello = new Hello();  // Hello: not in runtime classpath
          ...
      }

   }

}

The class Hello is optional for the application, so it may not be in runtime classpath.

When running the code, the JVM throws

java.lang.NoClassDefFoundError: Hello

At the line indicated before the constructor new Bar(false) is executed.

For lazy loading of classes, Hello.class should never be loaded.

Does Java HotSpot JVM use lazy loading of classes? lazy loading is JLS standard or implementation-specific?

How to workaround this issue? Reflection is not the right solution for us.

0 Answers
Related