Skip to Main Content

Java Programming

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.

ClassCastException with AppClassLoader and URLClassLoader in OpenJDK 17

Madhumita DasOct 24 2024

Hello Team,

I need to migrate our legacy application from java8 to java17. While running the application, I encountered the following exception with OpenJDK 17. This worked fine with Java 8, but the new class loader mechanism under the module system is causing issues. What should be the solution for this?
java -version
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment Temurin-17.0.11+9 (build 17.0.11+9)
OpenJDK 64-Bit Server VM Temurin-17.0.11+9 (build 17.0.11+9, mixed mode, sharing)

Exception in thread "main" java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')
at sentinel.command.SentinelBoot.addToClassPath(SentinelBoot.java:194)
at sentinel.command.SentinelBoot.initClassPath(SentinelBoot.java:63)
at sentinel.command.SentinelStarter.initStarter(SentinelStarter.java:54)
at sentinel.command.TrkServer.main(TrkServer.java:48)

Sharing few code snippets for reference:
URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Class<URLClassLoader> sysclass = URLClassLoader.class;
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[]{u});

I am stuck for so many days, nowhere getting any solutions. Please guide me. Thanks in advance!

Comments

Artan Hajdari Feb 11 2025

Can someone help me in this case please

1 - 1

Post Details

Added on Oct 24 2024
1 comment
610 views