eclipse/tomcat: deploy doesn't work any more (ClassNotFoundException)
Asked Answered
C

10

22

I'm running Eclipse Helios Service Release 1, with Tomcat 7.0.12 in Linux Ubuntu Natty Narwhal.

I've been happily hot re-deploying my webapp until it stopped working for apparently no reason. The following exception is displayed:

SEVERE: Allocate exception for servlet Index
java.lang.ClassNotFoundException: obliquid.servlet.Index
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
  • In Servers tab I've "Tomcat v7.0 Server at localhost [Started,Synchronized]
  • My project appears as a child of Tomcat v7.0 Server
  • In Properties, Java Build Path, Source I've Project/src Source folder
  • In Properties, Web Deployment Assembly, I've the following mappings: /WebContent -> /, /src -> /WEB-INF/classes, /test -> /build/classes
  • My src directory contains a Servlet in obliquid/servlet/Index.java
  • I tried already to click on Clean Module Work Directory... and Publish
  • I tried to stop and start the Server from within Eclipse Servers tab

What else should I check? Thank you.

UPDATE Despite now I'm working with the new project, I came back to check the old one, and mysteriously now it's working. I think I won't be able to find what has happened.

However today with the new project, I had 404 errors with no apparent reason and I found out that right clicking on the Tomcat server and selecting "Clean..." can be useful. Maybe it could have helped.

Selecting "Clean..." says: "Clean will discard all publish state and republish from scratch. Are you sure you want to clean all published resources?". Selecting yes, I solved the problem

UPDATE 2 It happened again on the new project. 404 errors, this time they don't go away.

Stop -> Clean... -> Start (404)
Stop -> Clean Tomcat Work Directory... -> Start (404)
Stop -> Clean Tomcat Work Directory... -> Clean... -> Start (404)
Stop -> Remove on the application ->  Clean... -> Run As -> Run on Server -> (404)  
Exit Eclipse, Start Eclipse
Start the server -> (404)

UPDATE 3 It turned out that this time I just didn't notice an exception caused by a listener-class during startup. After solving the problem, it worked. Guess I should stop working at 3 AM.

Cramp answered 9/5, 2011 at 9:26 Comment(5)
Try removing all projects, restarting Tomcat, then adding the project again.Isma
Thank you Sandman, I tried your suggestion: removed all projects from the Server, Restarted, then I choose Run As -> Run on Server, which prompted me to add the project, but still the same. Along this line I'm thinking to try to start a new Web Project, moving all source code and libraries and see if that help.Cramp
I've installed Eclipse Helios SR 2, made a new Dynamic Web Project with a different name, copied all source code, copied all JSPs and other resources, re-organized the libraries while at it, now it works... I thought mine was a stupid question, in fact I still don't know what went wrong.Cramp
Tomcat is terrible when it comes to hotdeployments. I always disable it and restart manually (by a keyboard shortcut). It takes only 3 seconds, almost as long as hotdeploys. Consider Glassfish, it is way much better in hotdeploying, it happens practically realtime.Pistoleer
Thank you BalusC, I will try it. Anyway in my case stop/start didn't help either, so I think something got messed up... actually now with the new project hot deploy works again and it's really fast. Will maybe remain a mistery this problem... I thought there was some checklist around.Cramp
A
37

While on Tomcat 6 and Eclipse Ganymede I found out the following chain to work like charm:

1 stop server

2 project -> clean

3 project build (I had automatic build disabled)

4 delete server

5 delete Servers folder

6 restart Eclipse

7 create new server, add project and start :)

takes some time but worked like charm. My problem was a irritating Listener start problem, but this seems to be something similar: a property at tomcat. Btw: nowadays I am also a big Glassfish fan.

Alica answered 21/5, 2011 at 20:47 Comment(9)
This was on Windows machine, but otherwise setup near to similarAlica
The problem happened again, and this time my little naughty dog unplugged my Linux laptop which is 5 years old, without battery. I tried part of your procedure, (stop server, project -> clean, (automatic rebuild), start server) and didn't work. So I tried (stop server, right click on server list -> Clean..., publish, start server) and it worked, but before I also tried "Clean Tomcat Work Directory...". I suspect, but can't prove for now that this sequence would work: (stop server, right click on server -> Clean Tomcat Work Directory..., right click on server -> Clean..., start server).Cramp
@stivlo: I don't say my way is the only way that works, but that I can say that those 7 steps of mine always make the trick. Good to hear about another possible paths, but as you said they should be verified first. I can say mine is verified already!Alica
Hi Mico! I just switched back your answer as accepted, because today my procedure failed and yours worked. My procedure worked in many occasions before, so it could be worth to give a try before yours since it's faster. It's very frustrating that problem is happening frequently both on Linux and Windows. I also wanted to try Glassfish a few days ago, but I had a problem, maybe you can give a look? Thanks!Cramp
Thank you. But why?! Why does this crap happen?! :(Burweed
@Vedran: Please look my new answer, how I survive with this nowadays.Alica
Deleting the server and re-creating it solved the problem for me. Yet, like some other contributors, I wonder what provokes such a dysfunction. +1 for mico.Knut
I don't know about that, but my second answer gives insight how to cope without getting this error. It has only one vote, thus scrolling down needed ;)Alica
I know the problem being discussed isn't quite the same as what I had in https://mcmap.net/q/587292/-tomcat-8-5-in-eclipse-4-7-0-not-seeing-changed-html-files/421049, but when @Alica noted in passing that he had disabled automatic build, that completely solved my separate problem. Thank you.Guidebook
C
12

I found that this procedure is useful:

  • Click on Servers tab and Stop the server in use if it's running
  • Right click on the server again and select Clean...
  • Right click again and select Clean Tomcat Work Directory...

Hopefully the ClassNotFoundException should be gone now.

Another time I had a problem with a class launched at server startup, an exception in a listener class (ServletContextListener). When a ServletContextListener raises an exception during startup the application deployment is aborted, hence the 404 errors. In that case fixing the problem that caused the exception, made the application working again.

EDIT: This shorter procedure worked for me most of the times, but today didn't work and I had to follow Mico's extended procedure. My suggestion is, if you have a similar problem, first try this shorter procedure. If the problem persists, try with Mico's.

Cramp answered 20/5, 2011 at 18:56 Comment(0)
L
2

I'd recommend that you stop and start the Tomcat server again. Hot deploy does not work forever; there are some issues that will cause you to have to restart after a few redeploys.

Leapfrog answered 9/5, 2011 at 9:31 Comment(1)
Thank you for your answer duffymo, but I tried that too... I've added it to the list in my original question.Cramp
L
2

I wonder when I see the accepted answer with +25 is it really accurate?

First if you are going to delete the server then whats the point in cleaning it? It will take your time unnecessarily and nothing useful will you gain.

So i will say just 5, 6, 7 steps should do the magic

5 delete Servers folder

6 restart Eclipse

7 create new server, add project and start

Luwian answered 7/11, 2013 at 7:29 Comment(4)
wassup with all the bold ?Mendacity
I generally use bold to highlight main points so that even if you don`t have time to read detailed answer user can fairly easily understand the solution.Luwian
This seems to be the quickest solution. However, to study the result of the operations, check out the folder where everything is copied: your-eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\ Sometime, when you create a new server a new tmpX folder will be created. Within these tmp-folders you'll find wtpwebapps or something like that - you should find all files published if everything works alright.Pastorate
even yours is overcomplicated... merely deleting and re-creating the server works fine... but this never-ending crap with Eclipse is annoying as hell :(Sectarianize
B
1

This could be something I learned on con-fess 2011. For me this sounds like a classloader problem.

The theory behind:

  • Java does not only use the type of the class but also the classloader which loaded it to identify the type of an instance. This means a simle operation could fail, e.g.

    ClassA a1 = new a1; ClassA a2 = SomeOtherClass.giveMeInstanceOfA(); a1 = a2;

This example would fail if SomeOtherClass uses a different classloader because Java would say that they are not the same.

  • The speaker also mentioned that some servers use per default about 45 different classloaders.

What does this mean in practice:

You deploy your webapp to the server, everything runs fine. The server caches the classes and everything he needs to load them somewhere. Now you make a hot deploymen and the server may loads new classes with a new (or different) classloader. This is the point where it starts to become dangerous because from now on you have two different classes in the memory which should be the same. Simple operations like casts (ClassA a = (ClassA) new ClassA()) fail, new Methods within the class are not found (because the server takes a cached version without this method),....

This is the point where I restart the server, clean the working directory (to get rid of the cached versions) and start thinking about hot deployment as critical thing.

If it is possible to try Mikkos procedere and this solves the problem this explanation could help you understand why.

I know this does not solve your problem but could give you some hints what maybe could have happened.

Bracing answered 26/5, 2011 at 6:57 Comment(1)
Interesting, thank you, looks like I want to know more about this subject now. I will research.Cramp
C
1

Speaking of hot deployment of Tomcat, you would indeed experience various problems, the least of which is memory leak, which is why you would have to restart the application. I would recommend to try JRebel for fast turnaround of "make and save any changes and then refresh the browser and see the changes immediately". You can find the JRebel hands-on lab, which shows how to use it with Tomcat and Eclipse. http://www.javapassion.com/rebels/jrebel_basics/

Chirography answered 27/5, 2011 at 16:29 Comment(0)
A
1

I have gained some new experience on programming and I am not anymore starving on Eclipse + Tomcat combination. Several ways here can help you out of the need of the usage of that combination:

First of all, don't use them together!

  • You can use other available IDEs, e.g. IntellijIdea (that is not free, but is worth of investing) has an property that when you debug a Java code, you can change code on fly and compile the Java files one by one and then it suggests if you'll want to have it updated on server. No restart needed almost never (of course it sometimes gets lost, but mainly it works).

  • Use standalone tomcat server, not the one inside Eclipse / your IDE. That first trick works only when you debug external server, nothing inside IDE. There comes the second tip: if you happen to change only jsp or html content, those files can be copied to the right place inside tomcat's webapp folder manually with unix cp or windows copy command and if you happen to develop files in same foulder long time, you can copy the folder content (e.g. myFolder/*.jsp) as many times as you wish and no restart is needed at all. The changes come visible when you touch or edit&save the web.xml file inside the webapps folder and then after that with refreshing the visible page on browser. Probably hard refresh with CTRL+F5 is the best way.

Thanks to @Verdan about his comment, otherwise I wouldn't have came back to answer again.

Alica answered 22/8, 2012 at 20:37 Comment(0)
S
1

I was able to solve this by disabling the maven nature (right click on project >> maven >> disable maven nature). Then re-enabling it (right click on project >> configure >> convert to maven project). I had tried all of the other tips and tricks above, but this is the one that finally worked.

Sachsse answered 2/6, 2017 at 5:44 Comment(0)
P
0

I was running into the same problem - tried all of the above, Eclipse always freezed when i tried to start the server, even after i deleted all the server configs and created a new one with a freshly downloaded tomcat instance. Anyways, problem wasnt solved until i moved to a new workspace, reimported the projects and created a new server. Seems like an Eclipse bug to me...So in case nothing else works, this is the way to go...

Padilla answered 7/5, 2012 at 8:41 Comment(0)
C
0

I'm currently struggling with the same problem, but nothing mentioned here does not help me. Anyway, I found out that if I:

  1. stop server in eclipse
  2. run tomcat elsewhere (in my case the xamp distribution)
  3. stop currently running tomcat
  4. start tomcat in eclipse

everything works just fine, of course until I change something in code and try to test it again.

Coenzyme answered 20/8, 2013 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.