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

折月煮酒 提交于 2020-01-18 09:50:12

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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/




回答8:


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...




回答9:


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.




回答10:


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.



来源:https://stackoverflow.com/questions/5934922/eclipse-tomcat-deploy-doesnt-work-any-more-classnotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!