Class MRJURLClassLoader

All Implemented Interfaces:
Closeable, AutoCloseable

public class MRJURLClassLoader extends URLClassLoader
Polyfill for the URLClassLoader regarding the fact that classloaders can be named in Java 9 and beyond.

In Java 8 this doesn't do much, but in Java 9+ the classloader's name will show up in stacktraces, which is very helpful for diagnosing certain classloading errors.

This classloader is parallel-capable.

  • Constructor Details

  • Method Details

    • getClassloaderName

      @Nullable public static final @Nullable String getClassloaderName(ClassLoader loader)
      Returns the name of the given classloader. For Java 8, this method only returns for classloaders that are an instance of MRJURLClassLoader. Otherwise, it returns null.

      For Java 9 and above, it returns a value for any ClassLoader, provided said ClassLoader has been assigned a name.

      Parameters:
      loader - The ClassLoader instance to get the name of.
      Returns:
      The name of the given loader.
    • getPlatformClassLoader

      @Nullable public static final @Nullable ClassLoader getPlatformClassLoader()
      Obtains the platform classloader under Java 9 or above. Under Java 8, null is returned.

      Keep in mind that while the platform classloader contains the platform classes, it has also access to modules on the modulepath. This means that when using JPMS/Jigsaw, it may load classes that are part of the 'app' classloader. This behaviour contradicts the behaviour for classes present on the classpath, which cannot be loaded through the platform classloader.

      Returns:
      The platform classloader, if present.
    • getName

      @NotNull public @NotNull String getName()
      Obtains this classloader's name.
      Overrides:
      getName in class ClassLoader
      Returns:
      The name of the classloader.