Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Java 8 code that casts the system class loader breaks on Java 11 and later

Your error

java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')1 more line it can show

Fix it fast

Most common cause, 1 of 3

The code adds JARs at runtime by calling addURL on the system loader

Stop changing the system loader.

Fix
URL[] urls = { Path.of("plugins/my-plugin.jar").toUri().toURL() };
URLClassLoader pluginLoader =
    new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
Class<?> type = Class.forName("com.example.MyPlugin", true, pluginLoader);

# or, when the JARs are known at launch:
java --class-path "app.jar:plugins/*" com.example.Main
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ClassCastException: jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to java.net.URLClassLoader appears because since JDK 9 the application class loader is an internal class, not a URLClassLoader. Code written for Java 8 that casts it to call addURL or getURLs fails. Create your own URLClassLoader, use Instrumentation.appendToSystemClassLoaderSearch in an agent, or read java.class.path. Applies to JDK 11 through 27.

  • Applies to JDK 25 (LTS), JDK 27, JDK 21 (LTS), JDK 11 (LTS)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Find the line that performs the cast. The first stack frame under the exception that belongs to your code, your agent or a library names the culprit; then search the source for the cast to see what it was trying to do.

Run this first
grep -rn "URLClassLoader)" src/ --include="*.java"
grep -rnE "getURLs\(\)|\"addURL\"" src/ --include="*.java"

Then find the line you see in this list and open its fix:

  1. 1The code adds JARs at runtime by calling addURL on the system loaderThe cast is followed by a reflective call to addURL, usually to load plugins or drivers that are not on the class path at start
  2. 2A Java agent puts its own JAR on the class path with the castThe stack trace runs through premain or agentmain, and the app was started with -javaagent
  3. 3The code reads the class path through getURLs()The cast is followed by getURLs(), for example in a class path scanner, a test helper or a tool that prints the class path

What does the error look like?

  • java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
  • java.lang.ClassCastException: jdk.internal.loader.ClassLoaders$AppClassLoader incompatible with java.net.URLClassLoader

Cause 1 of 3

The code adds JARs at runtime by calling addURL on the system loader

You seeThe cast is followed by a reflective call to addURL, usually to load plugins or drivers that are not on the class path at start

Check

On Java 8 the system class loader was a URLClassLoader, so a common trick was to cast it and call its protected addURL method through reflection. Oracle's JDK migration guide says the application class loader is now an instance of an internal class, so that cast throws ClassCastException on every release from JDK 9 on.

Check
java -version

Fix

Stop changing the system loader. Build a URLClassLoader of your own with the system loader as its parent, and load the plugin classes through it. If the JARs are known before start-up, list them on the class path instead.

Fix
URL[] urls = { Path.of("plugins/my-plugin.jar").toUri().toURL() };
URLClassLoader pluginLoader =
    new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
Class<?> type = Class.forName("com.example.MyPlugin", true, pluginLoader);

# or, when the JARs are known at launch:
java --class-path "app.jar:plugins/*" com.example.Main

Verify

The application starts on the newer JDK, and the plugin class is found without the exception.

Verify
java --class-path "app.jar:plugins/*" com.example.Main

Confirmed by docs.oracle.com

Did this fix it?

Cause 2 of 3

A Java agent puts its own JAR on the class path with the cast

You seeThe stack trace runs through premain or agentmain, and the app was started with -javaagent

Check

Agents written for Java 8 often cast the system loader so they can add helper JARs. Since Java 6 the instrumentation API has had a supported way to do this, and it keeps working after the class loader change.

Check
grep -rnE "premain|agentmain" src/ --include="*.java"

Fix

Use the Instrumentation object passed to premain or agentmain and call appendToSystemClassLoaderSearch with the JAR. Keep that JAR limited to the classes the agent needs, as the API docs warn.

Fix
public static void premain(String args, Instrumentation inst) throws IOException {
    inst.appendToSystemClassLoaderSearch(new JarFile("/opt/agent/agent-helpers.jar"));
}

Verify

The JVM starts with the agent attached, and classes from the helper JAR load from the system class loader.

Verify
java -javaagent:/opt/agent/my-agent.jar -jar app.jar

Confirmed by docs.oracle.com

Did this fix it?

Cause 3 of 3

The code reads the class path through getURLs()

You seeThe cast is followed by getURLs(), for example in a class path scanner, a test helper or a tool that prints the class path

Check

Some code casts the system loader only to list the JARs it searches. That information is still available without any cast: the java.class.path system property holds the class path the JVM was started with.

Check
java -cp app.jar -XshowSettings:properties -version 2>&1 | grep java.class.path

Fix

Read the system property and split it on the platform path separator. Note that JARs added later through appendToSystemClassLoaderSearch do not show up in this property.

Fix
String[] entries = System.getProperty("java.class.path")
    .split(java.io.File.pathSeparator);
for (String entry : entries) {
    System.out.println(entry);
}

Verify

The same list of JARs and folders prints on JDK 8 and on the newer JDK.

Verify
java -cp app.jar com.example.PrintClassPath

Confirmed by docs.oracle.com

Did this fix it?

How do I stop it coming back?

  • Run the test suite on the newest LTS JDK in CI alongside the one you ship on, so a cast like this fails in a build and not in production.
  • Search new dependencies and agents for casts to URLClassLoader before you adopt them.
  • Load plugins through a dedicated class loader from the start, which also lets you unload or replace them later.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.