URLClassLoader

Overview

The URLClassLoader is a subclass of ClassLoader that was widely used in Java 8 and earlier versions as the application class loader. It provides methods such as getURLs() to access the class path, which was a common requirement for many Java applications. However, with the introduction of Java 9, the class-loading mechanism underwent significant changes. The application class loader is now an AppClassLoader, and casting it to URLClassLoader is no longer possible. This change necessitates alternative approaches for accessing class path information in Java 9 and later.

Application Class Loader

In Java 8 and earlier, the application class loader was a URLClassLoader, which is a subtype of ClassLoader. Developers often cast the application class loader to URLClassLoader to utilize its specific methods, such as getURLs(). This practice was straightforward and effective for accessing the class path. However, in Java 9 and later, the application class loader is an AppClassLoader. Attempting to cast it to URLClassLoader will result in a class-cast exception, as AppClassLoader does not extend URLClassLoader.

Example of Casting to URLClassLoader

Here is an example of how casting to URLClassLoader was typically done in Java 8 and earlier:

Listing 6.1 Casting the application class loader to URLClassLoader

private String getClassPathContent() {
    URLClassLoader loader =
        (URLClassLoader) this.getClass().getClassLoader();    #1  
    return Arrays.stream(loader.getURLs())    #2  
            .map(URL::toString)
            .collect(joining(", "));
}
#1  Gets the application class loader and casts it to URLClassLoader
#2  getURLs doesn’t exist on ClassLoader, which is the reason for the cast.

Troublesome Casts in Java 9 and Later

When running a project on Java 9 or later, you may encounter a class-cast exception if your code attempts to cast an AppClassLoader to URLClassLoader. The JVM will complain about this invalid cast, as shown in the following example:

> Exception in thread "main" java.lang.ClassCastException:
>     java.base/jdk.internal.loader.ClassLoaders$AppClassLoader    #1  
>     cannot be cast to java.base/java.net.URLClassLoader    #2  
>         at monitor.Main.getClassPathContent(Main.java:46)
>         at monitor.Main.main(Main.java:28)
#1  The class loader returned by getClass is an AppClassLoader.
#2  AppClassLoader doesn’t extend URLClassLoader, so the cast fails.

This change in class-loading behavior is part of Java 9's efforts to improve launch performance. Even if your project does not currently suffer from this issue, understanding these changes can deepen your Java knowledge.

Getting Along Without URLClassLoader

In Java 9 and later, if you encounter a cast to URLClassLoader in a project, you have several options:

  • Open an Issue or Contribute a Fix: If the code is part of an open-source project, consider opening an issue or contributing a fix.
  • Fork or Patch Locally: You can fork the project or apply a local patch to address the issue.
  • Use Alternative Solutions: If your own code performs the casting, you may need to find alternative solutions. Options include using the system property java.class.path or creating a new class loader.

To identify troublesome casts to URLClassLoader, perform a full-text search for "(URLClassLoader)" in your codebase. This search should yield few false positives. For dependencies, you may need to use a combination of build-tool and command-line techniques to search through source JARs and their contents.

FAQ (Frequently asked questions)