Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Base ClassLoader No Longer from URLClassLoader

3388296Jan 29 2017 — edited Feb 7 2018

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());

This post has been answered by 3388296 on Jan 30 2017
Jump to Answer

Comments

Processing

Post Details

Added on Jan 29 2017
2 comments
73,721 views