0

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

Subhomoy Sikdar
  • 526
  • 5
  • 19

2 Answers2

0

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.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • The solution is not the correct one but the scenario is like this. The common classes which is used by all the war files are placed in tomcat lib and I have a need to get some data inside a class in tomcat lib which is same as done in a class in war file. I do not want to copy paste the same code but instantiate the class inside the war file and get the data – Subhomoy Sikdar Jan 25 '16 at 10:15
0

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.

dzidzitop
  • 385
  • 2
  • 11