Forum Stats

  • 3,838,710 Users
  • 2,262,394 Discussions
  • 7,900,739 Comments

Discussions

How to make my default classloader a URLClassloader in Java 11?

4ec39370-0a2c-4350-9d5a-9d7438667d94
edited Aug 11, 2020 2:54AM in Java Programming

I am trying to migrate from Java 8 to Java 11 and the most painful surprise was that the AppClassLoader (also known as the application or system classloader) can no longer be cast as a URLClassloader.

Worse, everything I found through google that refers to using a custom classloader are simple examples that show how to create a new classloader (yes, even extending URLClassloader) but stop there, with a manual instantiation of a class.  Nothing on how to make it so that all classes loaded *after* instantiating it to be loaded through *that* classloader instead of the previous default.

So I have a couple of questions:

1) Does the Tomcat 9 running on Java 11 *common* classloader (the one used by libraries) still return a URLClassloader?  I am guessing yes, since they built this whole container classloader hierarchy, so I am hoping they used the URLClassloader as their base.  But if no, can someone recommend a different web application server that relies on URLClassLoaders?

2) For command line applications this is even more critical.  I need to create a URLClassloader (with the App class loader as its parent) and then make it the default for the rest of the application classloading.  Can someone point me to a good guide for this kind of design?