Linux eclipse/tomcat:部署不再起作用(ClassNotFoundException)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5934922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:51:59  来源:igfitidea点击:

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

javalinuxeclipsetomcatclassnotfoundexception

提问by stivlo

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

我正在运行 Eclipse Helios Service Release 1,在 Linux Ubuntu Natty Narwhal 中使用 Tomcat 7.0.12。

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

我一直很高兴地重新部署我的 web 应用程序,直到它显然无缘无故停止工作。显示以下异常:

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 Serverstab 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/srcSource folder
  • In Properties, Web Deployment Assembly, I've the following mappings: /WebContent-> /, /src-> /WEB-INF/classes, /test-> /build/classes
  • My srcdirectory 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
  • 服务器选项卡中,我有“本地主机上的 Tomcat v7.0 服务器 [已启动,已同步]
  • 我的项目显示为 Tomcat v7.0 Server 的子项
  • 在属性、Java 构建路径、源中我有Project/src源文件夹
  • 在属性,Web 部署程序集,我有以下映射:/WebContent-> //src-> /WEB-INF/classes/test-> /build/classes
  • 我的src目录在obliquid/servlet/Index.java 中包含一个 Servlet
  • 我已经尝试点击Clean Module Work Directory...发布
  • 我试图从 Eclipse 服务器选项卡中停止和启动服务器

What else should I check? Thank you.

我还应该检查什么?谢谢你。

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

然而,今天在新项目中,我无缘无故地出现了 404 错误,我发现右键单击 Tomcat 服务器并选择“清理...”可能很有用。也许它可以有所帮助。

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 2It happened again on the new project. 404 errors, this time they don't go away.

更新 2它再次发生在新项目上。404 错误,这次不会消失。

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

UPDATE 3原来这一次我只是没有注意到启动过程中侦听器类引起的异常。解决问题后,它起作用了。我猜我应该在凌晨 3 点停止工作。

采纳答案by mico

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

在 Tomcat 6 和 Eclipse Ganymede 上,我发现以下链可以像魅力一样工作:

1 stop server

1站服务器

2 project -> clean

2 项目 -> 清洁

3 project build (I had automatic build disabled)

3 项目构建(我禁用了自动构建)

4 delete server

4 删除服务器

5 delete Servers folder

5 删除Servers文件夹

6 restart Eclipse

6 重启Eclipse

7 create new server, add project and start :)

7 创建新服务器,添加项目并启动:)

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.

需要一些时间,但工作起来很有魅力。我的问题是一个恼人的 Listener 启动问题,但这似乎是类似的东西:tomcat 的一个属性。顺便说一句:现在我也是 Glassfish 的忠实粉丝。

回答by duffymo

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.

我建议您停止并再次启动 Tomcat 服务器。热部署不会永远工作;有一些问题会导致您在几次重新部署后必须重新启动。

回答by stivlo

I found that this procedure is useful:

我发现这个过程很有用:

  • Click on Serverstab 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...
  • 如果服务器正在运行,请单击服务器选项卡并停止使用中的服务器
  • 再次右键单击服务器并选择清理...
  • 再次右键单击并选择Clean Tomcat Work Directory...

Hopefully the ClassNotFoundException should be gone now.

希望 ClassNotFoundException 现在应该消失了。

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.

还有一次我遇到了在服务器启动时启动的类的问题,即侦听器类 (ServletContextListener) 中的异常。当 ServletContextListener 在启动期间引发异常时,应用程序部署将中止,因此会出现 404 错误。在这种情况下,修复导致异常的问题,使应用程序再次运行。

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.

编辑:这个较短的程序在大多数情况下对我有用,但今天不起作用,我不得不遵循 Mico 的扩展程序。我的建议是,如果您有类似的问题,请先尝试这个较短的程序。如果问题仍然存在,请尝试使用 Mico's。

回答by monty

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

这可能是我在confess 2011中学到的东西。对我来说,这听起来像是一个类加载器问题。

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;

  • Java 不仅使用类的类型,还使用加载它的类加载器来识别实例的类型。这意味着一个简单的操作可能会失败,例如

    ClassA a1 = 新 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.

如果 SomeOtherClass 使用不同的类加载器,则此示例将失败,因为 Java 会说它们不相同。

  • The speaker also mentioned that some servers use per default about 45 different classloaders.
  • 发言人还提到一些服务器默认使用大约 45 个不同的类加载器。

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

您将 web 应用程序部署到服务器,一切正常。服务器缓存类以及他需要将它们加载到某处的所有内容。现在您进行热部署,服务器可能会使用新的(或不同的)类加载器加载新类。这是它开始变得危险的地方,因为从现在开始,您的内存中有两个不同的类,它们应该是相同的。像强制转换 (ClassA a = (ClassA) new ClassA()) 之类的简单操作失败,找不到类中的新方法(因为服务器采用没有此方法的缓存版本),....

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.

如果可以尝试 Mikkos procedere 并且这解决了问题,则此解释可以帮助您理解原因。

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

我知道这不能解决您的问题,但可以给您一些提示,可能会发生什么。

回答by Sang Shin

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/

说到Tomcat的热部署,确实会遇到各种各样的问题,最不重要的就是内存泄漏,这就是为什么你必须重新启动应用程序的原因。我建议尝试 JRebel 以快速完成“进行并保存任何更改,然后刷新浏览器并立即查看更改”。您可以找到 JRebel 动手实验室,其中展示了如何将它与 Tomcat 和 Eclipse 结合使用。http://www.javapassion.com/rebels/jrebel_basics/

回答by user871784

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

我遇到了同样的问题 - 尝试了上述所有方法,当我尝试启动服务器时 Eclipse 总是冻结,即使我删除了所有服务器配置并使用新下载的 tomcat 实例创建了一个新配置。无论如何,直到我搬到一个新的工作区,重新导入项目并创建一个新的服务器,问题才得到解决。对我来说似乎是一个 Eclipse 错误......所以如果没有其他工作,这就是要走的路......

回答by mico

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:

我在编程方面获得了一些新经验,我不再对 Eclipse + Tomcat 组合感到饥饿。这里有几种方法可以帮助您摆脱使用该组合的需要:

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.

  • 您可以使用其他可用的 IDE,例如 IntellijIdea(它不是免费的,但值得投资)具有一个属性,即当您调试 Java 代码时,您可以动态更改代码并逐个编译 Java 文件,然后它会建议如果你想在服务器上更新它。几乎从不需要重启(当然它有时会丢失,但主要是有效的)。

  • 使用独立的 tomcat 服务器,而不是 Eclipse/IDE 中的服务器。第一个技巧仅在您调试外部服务器时才有效,在 IDE 中没有任何作用。还有第二个技巧:如果您碰巧只更改了jsp或html内容,则可以使用unix cp或windows copy命令手动将这些文件复制到tomcat的webapp文件夹中的正确位置,如果您碰巧在同一文件夹中长时间开发文件,您可以根据需要多次复制文件夹内容(例如 myFolder/*.jsp),并且根本不需要重新启动。当您在 webapps 文件夹中触摸或编辑并保存 web.xml 文件,然后刷新浏览器上的可见页面时,这些更改就会可见。使用 CTRL+F5 硬刷新可能是最好的方法。

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

感谢@Verdan 关于他的评论,否则我不会再回来回答了。

回答by vanomart

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
  1. 在 Eclipse 中停止服务器
  2. 在别处运行 tomcat(在我的例子中是 xamp 发行版)
  3. 停止当前运行的tomcat
  4. 在eclipse中启动tomcat

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

一切正常,当然,直到我更改代码中的某些内容并尝试再次对其进行测试。

回答by Amol Patil

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

我想知道当我看到+25接受的答案时,它真的准确吗?

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

首先,如果您要删除服务器,那么清理它有什么意义呢?这将不必要地花费您的时间,并且您不会获得任何有用的东西。

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

所以我会说只有5、6、7 个步骤应该可以发挥作用

5 delete Servers folder

5 删除Servers文件夹

6 restart Eclipse

6 重启Eclipse

7 create new server, add project and start

7 创建新服务器,添加项目并启动

回答by Bobby King

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.

我能够通过禁用 Maven 特性来解决这个问题(右键单击项目 >> Maven >> 禁用 Maven 特性)。然后重新启用它(右键单击项目 >> 配置 >> 转换为 Maven 项目)。我已经尝试了上述所有其他提示和技巧,但这是最终奏效的方法。