Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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] Tue, 11 August 2020 01:18 Go to next message
Bruno Melloni is currently offline Bruno MelloniFriend
Messages: 6
Registered: August 2020
Junior Member
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 16:52]

Report message to a moderator

Re: Classloader woes after leaving Java 8. [message #1831264 is a reply to message #1831058] Sun, 16 August 2020 06:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31543
Registered: July 2009
Senior Member
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.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Classloader woes after leaving Java 8. [message #1831286 is a reply to message #1831264] Sun, 16 August 2020 17:40 Go to previous messageGo to next message
Bruno Melloni is currently offline Bruno MelloniFriend
Messages: 6
Registered: August 2020
Junior Member
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 23:59 Go to previous message
Bruno Melloni is currently offline Bruno MelloniFriend
Messages: 6
Registered: August 2020
Junior Member
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 22:08]

Report message to a moderator

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


Current Time: Thu Oct 22 16:06:40 GMT 2020

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

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

Back to the top