0

I have a web application and I am loading a class using Class.forname("com.test.abc"), and this abc class is present in different jar, all jars are present in web-inf/lib folder. but still i am getting ClassNotFoundException.

How does Class.forname() works in java? where does it look for the classes?

why I am getting this error? Do I need to add jar where abc is present into maven dependency or manifest file of the jar?

Sai
  • 21
  • 6
XYZ
  • 27
  • 6
  • 1
    when are getting error? can you share more details on how are you running the application? – greengreyblue Nov 05 '18 at 05:53
  • during run time, this is a web application – XYZ Nov 05 '18 at 05:57
  • `abc` is a class or a package? – Ori Marko Nov 05 '18 at 06:01
  • yes, it is there in web-inf/lib folder – XYZ Nov 05 '18 at 06:03
  • @user7294900 abc is class – XYZ Nov 05 '18 at 06:04
  • is it web-inf/lib is part of project structure or are the jar's present under web-inf/lib after deployment? – greengreyblue Nov 05 '18 at 06:04
  • after deplyment its present in web-inf/lib – XYZ Nov 05 '18 at 06:06
  • It should work in that case. you could verify if the class is present in the jar or not. is your class name is `abc` or `Abc`. Verify the package path is correct. – greengreyblue Nov 05 '18 at 06:37
  • It will use the thread's current classloader if you do not specify one. Where is the code you are loading the class from located? It will for example *not* be able to see the class if you try to load from a jar file that is part of the container. Can you try to use a classloader that can definitely see web-inf/lib? (`SomeClassInTheWebapp.class.getClassLoader()`) – Thilo Nov 05 '18 at 06:52
  • @Thilo how do i figure out, which classloader can see web-inf/lib – XYZ Nov 05 '18 at 07:07
  • Take one of your own classes and then do `SomeClassInTheWebapp.class.getClassloader()`. Pass this classloader to `Class.forName` (it has a version that takes such a parameter). – Thilo Nov 05 '18 at 07:17
  • But why do you need to do `Class.forName` anyway? Cannot you compile against that jar file? Does it really have to be added afterwards? – Thilo Nov 05 '18 at 07:18

0 Answers0