aspose file tools*
The moose likes Servlets and the fly likes ClassNotFoundException is very odd Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ClassNotFoundException is very odd" Watch "ClassNotFoundException is very odd" New topic
Author

ClassNotFoundException is very odd

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi there,

I have a servlet called twittersearch.web.TwitterSearchServlet which uses some classes in my twittersearch.model package.



Dispite the fact that all the classes that are used by my servlet have compiled fine and that all the necessary jars and file are in my classpath, I am getting a status 500 warning that says that it is getting a ClassNotFoundException : com.colorfulsoftware.atom.FeedDoc.

The FeedDoc class is used internally by the DownloadSearch class referend by my servlet above. Here's what I find strange:

1. If it can't find the FeedDoc class, then how come it knows that it's in the com.colorfulsoftware.atom package?
2. It must be able to find the com.colorfulsoftware.atom package because there's lots of other classes that are used by the DownloadSearch class from that package that aren't mentioned.
3. If I change the servlet code to comment out everything except to add a couple of reference variables of classes in the com.colorfulsoftware.atom package, including FeedDoc, then I don't get an error message:



this is the error message:



Does anybody know what I could be doing wrong?

Thanks

Joe


OCPJP 85%
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are missing the class com.colorfulsoftware.atom.FeedDoc from your classpath. It appears to be part of Atom, so you'll need to make sure the jar file(s) for that API are on your classpath.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Hi Paul,

Thanks for your reply. This is what I find strange. The jar file is in my classpath. I have just checked again and it is. When I jar -tf the jar file I can see the FeedDoc class is in the jar file. When I compile the DownloadSearch class which uses a FeedDoc object, I do not need to use the -classpath flag and it compiles fine.

Also why should Tomcat throw an exception about not being able to load the class if the DownloadSearch class is using it, when it doesn't throw an exception when the FeedDoc class is actually used in the servlet as in my second example. Its weird.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Joe --

Be clear that the library is missing at runtime, not at compile time. The jar is obviously being found from your classpath when the code is being compiled, but Tomcat won't look there. Is the jar file installed in Tomcat (i.e., in the web application itself?) Otherwise, it won't be found when the servlet is running. In your second example, the class isn't actually needed at runtime, so the missing jar won't hurt anything.


[Jess in Action][AskingGoodQuestions]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I have just checked again and it is. When I jar -tf the jar file I can see the FeedDoc class is in the jar file. When I compile the DownloadSearch class which uses a FeedDoc object, I do not need to use the -classpath flag and it compiles fine.

Well the exception tells you it is not. Where do you deploy that jar file to?
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Oh of course. I had Tomcat deployed on my machine rather than a separate server and that made me think that my machine's classpath locations would be used. I almost feel a bit silly.

I've put the jar in the Tomcat directory's lib directory (I assume that's where they're meant to go). It's working anyway.

Thank you both.

I'm not 100% clear as to why Tomcat wouldn't need the jar in my second example isn't needed when there are references of types that are in the jar file. Is it because they are only referencing null? I'm guessing that null references must be the same for all classes and so don't require the class to be loaded, but once you try to assign an object to them, then you need the class to be loaded. Is that right?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Joe Lemmer wrote: Is it because they are only referencing null? I'm guessing that null references must be the same for all classes and so don't require the class to be loaded, but once you try to assign an object to them, then you need the class to be loaded. Is that right?


Yep, that's it. The class file that's generated by the compiler won't contain any reference to that library class at all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Joe Lemmer wrote:1. If it can't find the FeedDoc class, then how come it knows that it's in the com.colorfulsoftware.atom package?

To answer this part, because every class reference is fully-qualified. Import statements are syntactic sugar.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

Joe Lemmer wrote:I've put the jar in the Tomcat directory's lib directory (I assume that's where they're meant to go). It's working anyway.


To get a little nit-picky : Instead of putting the JAR inside of Tomcat's lib directory, you should put the JAR in your application's WEB-INF/lib directory. That way, the JAR will be packaged up within the application's WAR file. So, if you ever have to reinstall Tomcat or have to deploy your application to a different Tomcat installation, you don't have to worry about separately copying that JAR.


SCJP 6 || SCWCD 5
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Thank you all for the good info.
 
Don't get me started about those stupid light bulbs.
 
subject: ClassNotFoundException is very odd