Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFoundException is very odd

 
Joe Lemmer
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
Eclipse IDE Java PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the good info.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic