Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 235 Big Data Appliance
- 1.9K Data Science
- 449.9K Databases
- 221.6K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 549 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 532 SQLcl
- 4K SQL Developer Data Modeler
- 186.9K SQL & PL/SQL
- 21.3K SQL Developer
- 295.5K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.1K Development Tools
- 104 DevOps
- 3.1K QA/Testing
- 645.9K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 158 Java 8 Questions
- 85.9K Java Programming
- 79 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.2K Java SE
- 13.8K Java Security
- 203 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 402 LiveLabs
- 37 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.6K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 230 Portuguese
Base ClassLoader No Longer from URLClassLoader

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());
Best Answer
-
This is due to the Java 9 architecture precluding use of URLClassLoader as a superclass for the system ClassLoaders.
I implemented the following changes with noted caveats:
Add the ClassPath property to the jar Manifest file to provide startup classpath additions.
When an extended classpath is required, I use:
Class.forName("nameofclass", true, new URLClassLoader(urlarrayofextrajarsordirs));
The above line also attaches the extended classpath to nameofclass so any other class definitions in nameofclass will also resolve.
Caveats:
java.util.ServiceLoader uses the thread's ClassLoader context Thread.currentThread().setContextClassLoader(specialloader);
java.sql.DriverManager does honors the calling class' ClassLoader, -not- the Thread's ClassLoader. Create Driver directly using Class.forName("drivername", true, new URLClassLoader(urlarrayofextrajarsordirs).newInstance();
javax.activation uses the thread's ClassLoader context (important for javax.mail).
Hopefully this will help the thousands of other authors who have relied on the application ClassLoader being a subclass of URLClassLoader.
Further elaboration on java.sql.DriverManager issue. While Class.forName(..., true, classloader) properly loads the class for the JDBC driver and registers it with DriverManager, it is unusable via DriverManager because DriverManager tries Class.forName(jdbcdrivername, true, caller.getClassLoader()) which fails since class caching is done at the child ClassLoader level, not the parent level.
Answers
-
This is due to the Java 9 architecture precluding use of URLClassLoader as a superclass for the system ClassLoaders.
I implemented the following changes with noted caveats:
Add the ClassPath property to the jar Manifest file to provide startup classpath additions.
When an extended classpath is required, I use:
Class.forName("nameofclass", true, new URLClassLoader(urlarrayofextrajarsordirs));
The above line also attaches the extended classpath to nameofclass so any other class definitions in nameofclass will also resolve.
Caveats:
java.util.ServiceLoader uses the thread's ClassLoader context Thread.currentThread().setContextClassLoader(specialloader);
java.sql.DriverManager does honors the calling class' ClassLoader, -not- the Thread's ClassLoader. Create Driver directly using Class.forName("drivername", true, new URLClassLoader(urlarrayofextrajarsordirs).newInstance();
javax.activation uses the thread's ClassLoader context (important for javax.mail).
Hopefully this will help the thousands of other authors who have relied on the application ClassLoader being a subclass of URLClassLoader.
Further elaboration on java.sql.DriverManager issue. While Class.forName(..., true, classloader) properly loads the class for the JDBC driver and registers it with DriverManager, it is unusable via DriverManager because DriverManager tries Class.forName(jdbcdrivername, true, caller.getClassLoader()) which fails since class caching is done at the child ClassLoader level, not the parent level.
-
"Hopefully this will help the thousands of other authors ..."
NO, it most certainly does not help at all.
This "answer" does *NOT* answer the questions or address the issues.
The OP asked how to fix some code, the "answer" contains vague suggestions and gobbledygook.
Answer the OP's questions. What does the OP have to do to fix their code ?
I have searched extensively on this issue, found dozens of threads, and NONE answer the issues or contain any fixes that work. Examples:
* Java example with ClassLoader
https://stackoverflow.com/questions/9691855/java-example-with-classloader
* Java 9, compatability issue with ClassLoader.getSystemClassLoader
https://stackoverflow.com/questions/46694600/java-9-compatability-issue-with-classloader-getsystemclassloader
* Java JDBC connection error with JDK 9
https://stackoverflow.com/questions/46828219/java-jdbc-connection-error-with-jdk-9?noredirect=1
* How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9
https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j
* Add jar to classpath at runtime under java 9
https://stackoverflow.com/questions/48041764/add-jar-to-classpath-at-runtime-under-java-9
* Base ClassLoader No Longer from URLClassLoader - SHODOV
https://community.oracle.com/thread/4011800
* Java 9 Migration Guide: The Seven Most Common Challenges - nipa 24.07.2017
https://blog.codefx.org/java/java-9-migration-guide/#Casting-To-URL-Class-LoaderSo, can anyone actually fix the OP's code ? Or do the 1,000's of us having problems with Java 9 just go back to using Java 8 - a product that actually worked and didn't stuff things up for no reason.
Hopefully this will help the thousands of other authors