Video in progress

What is the hierarchy of the standard ClassLoaders in Java, and how does "parent delegation" work?

There are three main loaders: Bootstrap, Platform (Extension in Java 8) and Application. When a class is requested, a loader first asks its parent and loads it itself only if the parent can't find it.

📖 Answer

The chain in Java 9+:

  • Bootstrap: inside the JVM, written in native code. java.base (String, Object, List) is loaded from here. From Java's point of view, getClassLoader() returns null.
  • Platform: the other JDK modules (java.sql, java.xml ...). In Java 8 the Extension loader was in its place.
  • Application (System): the classes on your classpath/module path.

The parent delegation order:

Application -> "Platform, do you have it?" -> "Bootstrap, do you have it?"
Bootstrap didn't find it -> Platform searches -> didn't find it -> Application loads it itself

Why it is done this way: you can't write your own java.lang.String class and replace the standard one. And a class is loaded once, so copies don't get mixed up.

Some environments break this rule on purpose: for example, Tomcat's web application loader first looks in the application's own WEB-INF/lib, so each application can use its own library versions.