What are the changes of class loaders in Java 9?


All java programs run on Java Virtual Machine (JVM). After compilation, a java class gets transformed into a platform and machine-independent bytecode, and compiled classes are stored as .class files. Whenever we try to use it, ClassLoader loads that class into memory. The classes get introduced into the Java environment when they are referenced by name. The loading of classes has been done by the class loader, once the class starts running, and the main() method is a way to start that class.

There are few minor changes of class loaders in Java 9:

  • The system class loader is no more in Java 9, an instance of URLClassLoader, rather it's an internal class. It is the default loader for classes in modules.
  • The extension class loader has renamed as a platform class loader. All classes in Java SE Platform can be visible through the platform class loader, and also classes in modules under the Java community process but not part of the Java SE platform also visible through the platform class loader.
  • Applications can't depend on the class loader that defines in which platform class of some classes in Java SE Platform are defined by platform class loader, while others are defined by the bootstrap class loader.
  • If the existing code creates a class loader with the bootstrap class loader as parent class loader, then we need to change to use platform class loader as the parent.
  • The platform class loader is not an instance of URLClassLoader, rather it's an internal class.
  • The bootstrap class loader is a built-in class loader of JVM. However, it defines the classes of critical modules such as a base. The applications deployed with -Xbootclasspath/a or create a class loader with null as a parent may need to change.

Example

public class ClassLoaderTest {
   public static void main(String args[]) {
      System.out.println("Class Loader Test");
      ClassLoaderTest test = new ClassLoaderTest();
      try {
         test.showClassLoaders();
      } catch(ClassNotFoundException cnfe) {
         System.out.println(cnfe.getMessage());
      }
   }
   public void showClassLoaders() throws ClassNotFoundException {
      System.out.println("Classloader of this class: " + ClassLoaderTest.class.getClassLoader());
      System.out.println("Classloader of Permission: " + java.sql.SQLPermission.class.getClassLoader());
      System.out.println("Classloader of LinkedList: " + java.util.LinkedList.class.getClassLoader());
      return;
   }
}

Output

Class Loader Test
Classloader of this class: jdk.internal.loader.ClassLoaders$AppClassLoader@504bae78
Classloader of Permission: jdk.internal.loader.ClassLoaders$PlatformClassLoader@299a06ac
Classloader of LinkedList: null

Updated on: 29-Apr-2020

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements