Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Classloader woes after leaving Java 8.(AppClassLoader (the Application or System classloader) can no longer be cast to URLClassLoader)
Classloader woes after leaving Java 8. [message #1831058] Mon, 10 August 2020 21:18 Go to next message
Eclipse UserFriend
I am migrating from Java 8 to 11 and slowly resolving the many issues that popped up. The latest issue is figuring out how make the default classloader for my application be a URLClassLoader. In Java 8 you could just cast it, since Java 9 you cannot.

I realize this is more of a generic Java 11 question than an Eclipse specific question, but I expect that this is a common issue and hoping someone here already knows the answer or can point me to somewhere where I can find it.

1) In pure java, there are many tutorials and threads on how to create a class that inherits from a URLClassLoader, but none on how to make it the *default* for subsequent code. How can I do that?

2) I *suspect* that since Tomcat has a custom classloader tree, Tomcat 9 probably still uses a URLClassLoader from its *common* classloader and up the stack. Does anybody know if this is true?
EDIT - CONFIRMED : Tomcat 9 uses URLClassLoader for the *common* and *webapps* classloaders.

[Updated on: Thu, 13 August 2020 12:52] by Moderator

Re: Classloader woes after leaving Java 8. [message #1831264 is a reply to message #1831058] Sun, 16 August 2020 02:09 Go to previous messageGo to next message
Eclipse UserFriend
Since no one else seems inclined to answer, one trick that's often used is the type of pattern you'll see here:

https://git.eclipse.org/c/equinox/rt.equinox.bundles.git/tree/bundles/org.eclipse.equinox.http.jetty/src/org/eclipse/equinox/http/jetty/internal/HttpServerManager.java#n272

I.e., using java.lang.Thread.setContextClassLoader(ClassLoader) to set the class loader used by that thread.
Re: Classloader woes after leaving Java 8. [message #1831286 is a reply to message #1831264] Sun, 16 August 2020 13:40 Go to previous messageGo to next message
Eclipse UserFriend
Ed Merks wrote on Sun, 16 August 2020 06:09
Since no one else seems inclined to answer, one trick that's often used is the type of pattern you'll see here:

https://git.eclipse.org/c/equinox/rt.equinox.bundles.git/tree/bundles/org.eclipse.equinox.http.jetty/src/org/eclipse/equinox/http/jetty/internal/HttpServerManager.java#n272

I.e., using java.lang.Thread.setContextClassLoader(ClassLoader) to set the class loader used by that thread.


Thank you! This is definitely one thing I needed. Worth its weight in gold.
Re: Classloader woes after leaving Java 8. [message #1831354 is a reply to message #1831264] Mon, 17 August 2020 19:59 Go to previous message
Eclipse UserFriend
Ed Merks wrote on Sun, 16 August 2020 06:09
Since no one else seems inclined to answer, one trick that's often used is the type of pattern you'll see here:

https://git.eclipse.org/c/equinox/rt.equinox.bundles.git/tree/bundles/org.eclipse.equinox.http.jetty/src/org/eclipse/equinox/http/jetty/internal/HttpServerManager.java#n272

I.e., using java.lang.Thread.setContextClassLoader(ClassLoader) to set the class loader used by that thread.


UPDATE:

1) The approach you kindly proposed worked for me with vanilla java but it breaks when loading a Spring context.xml.

2) I even customized ClassPathXmlApplicationContext() to try to force Spring to use my classloader when loading the context, but still it does not seem to work as classes from indirect dependencies of libraries used by things in the context fail to be found.

I have a more complete explanation of the problem here: https://stackoverflow.com/questions/63490683/spring-ignoring-the-classloader but sadly nobody seems to have a workable answer.

It is very puzzling, as migrating from Java 8 to newer versions of Java should have created similar problems for lots of people. And yet there is very little information on how to address the issue properly.

[Updated on: Wed, 19 August 2020 18:08] by Moderator

Previous Topic:Loading Jboss EAP runtime modules in Eclipse(June, 2020)
Next Topic:Raspbian Installation
Goto Forum:
  


Current Time: Fri Jul 25 01:11:18 EDT 2025

Powered by FUDForum. Page generated in 0.02557 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top