2 Replies Latest reply on Feb 8, 2018 2:56 AM by MooseV8

    Base ClassLoader No Longer from URLClassLoader

    3388296

      Since 1.1, the way to programmatically add JAR files to a class path was to get the default ClassLoader and cast it to URLClassLoader to invoke the AddURL method.  This is now broken in OpenJDK 9.  Here is the info:

       

      openjdk-9-jre-headless.

      openjdk version "9-Ubuntu"

      OpenJDK Runtime Environment (build 9-Ubuntu+0-9b134-2ubuntu1) OpenJDK Server VM (build 9-Ubuntu+0-9b134-2ubuntu1, mixed mode)

       

       

      Exception in thread "main" java.lang.RuntimeException: java.lang.ClassCastException: jdk.internal.loader.ClassLoaders$AppClassLoader (in module: java.base) cannot be cast to java.net.URLClassLoader (in module: java.base)

      ...

      Caused by: java.lang.ClassCastException: jdk.internal.loader.ClassLoaders$AppClassLoader (in module: java.base) cannot be cast to java.net.URLClassLoader (in module: java.base)

              ... 2 more

       

      And here is the code (yes, it is in a try/catch and newpath is already sanity checked):

       

                  final URLClassLoader urlClassLoader = (URLClassLoader) MyClass.class.getClassLoader();

                  final Method addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);

                  addURL.setAccessible(true);

                  addURL.invoke(urlClassLoader, new File(newpath.trim()).toURI().toURL());