ClassNotFoundException inside jar

G

Gaurav

Hi,
I have a method getData() in class DataAccess which internally calls a
method in a class inside a jar. The method inside the jar has
somehting like this:

Class.getForName("mypackage.myclass").newInstance();

The class instantiation works perfectly fine when I call getData()
from a main() method inside DataAcess. But I get a
ClassNotFoundException when I try to access the getData() method from
a web application (servlet container-Tomcat).

For the sake of debugging I added the line
Class.getForName("mypackage.myclass").newInstance(); inside the
getData() method itself. I observed there was no error when this line
was executed in getData() even when accessing the method from the
servlet.

Thus I only get the ClassNot FoundException inside the jar when I try
to access the method from the web application.

Someone suggested that this could be a classloader issue as there are
multiple classloaders in case of servlet but I am not really sure how
to resolve it.

I would be grateful if someone could provide some suggestions.

Note: The class in the jar reads the class name (mypackage.myclass)
from an XML file but I did put a debugger and observed that the
correct name was being sent to the jar.

TIA,
Gaurav
 
M

Manish Pandit

Hi,
I have a method getData() in class DataAccess which internally calls a
method in a class inside a jar. The method inside the jar has
somehting like this:

Class.getForName("mypackage.myclass").newInstance();

The class instantiation works perfectly fine when I call getData()
from a main() method inside DataAcess. But I get a
ClassNotFoundException when I try to access the getData() method from
a web application (servlet container-Tomcat).

For the sake of debugging I added the line
Class.getForName("mypackage.myclass").newInstance(); inside the
getData() method itself. I observed there was no error when this line
was executed in getData() even when accessing the method from the
servlet.

Thus I only get the ClassNot FoundException inside the jar when I try
to access the method from the web application.

Someone suggested that this could be a classloader issue as there are
multiple classloaders in case of servlet but I am not really sure how
to resolve it.

I would be grateful if someone could provide some suggestions.

Note: The class in the jar reads the class name (mypackage.myclass)
from an XML file but I did put a debugger and observed that the
correct name was being sent to the jar.

TIA,
Gaurav

Can you try putting the jar file in the WEB-INF/lib folder of your
webapp?

-cheers,
Manish
 
G

Gaurav

Tha jar is in thw WEB-INF/lib folder. I am not getting classNotFound
for the jar. But inside the class in jar there is a line

Class.getForName("mypackage.myclass").newInstance();

and it is for mypackage.myclass that I get a ClassNotFoundException.
mypackage.myclass resides under WEB-INF/lib.

Regards,
Gaurav
 
M

Manish Pandit

Tha jar is in thw WEB-INF/lib folder. I am not getting classNotFound
for the jar. But inside the class in jar there is a line

Class.getForName("mypackage.myclass").newInstance();

and it is for mypackage.myclass that I get a ClassNotFoundException.
mypackage.myclass resides under WEB-INF/lib.

Regards,
Gaurav





- Show quoted text -

Interesting..how about putting mypackage.myclass in the WEB-INF/
classes folder instead of WEB-INF/lib?

-cheers,
Manish
 
R

Roedy Green

I would be grateful if someone could provide some suggestions.

you want to look at some system properties. See
http://mindprod.com/applets/wassup.html
to look at the classpath and the ext directory.

Dump them out at both places and look for differences. Perhaps each
war file has its own classpath and these two are in different war
files.

use Class.getClassLoader to get a handle to the class loader then use
its various methods to help you figure out where it came from and if
it differs from the other one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top