Instantiating a tomcat WEB-INF\class\myclass from tomcat lib using classloader

106 Views Asked by At

I am trying to load a class present in a war file in tomcat from a class in a jar in tomcat lib. I am doing this using Class.forname("myclass") but the application is throwing class not found exception. i guess this is because the webapp will have its own class loader. can someone suggest how to fix this

2

There are 2 best solutions below

1
On

This is by design - you can't do that. With the hierarchical classloaders that tomcat is using, you don't have a chance to load a class that's contained a webapp from a class loaded from the global classpath unless you build your own classloading mechanism - which I wouldn't recommend.

I'd rather like to know what problem you're trying to solve with this solution attempt. I can't think of any underlying problem where this would be the right solution for.

2
On

You should use Thread#getContextClassLoader() to instantiate classes that could be a part of your WAR distribution. But check that Tomcat initialises it with the web app classloader.