java.lang.ClassNotFoundException: eclipse 中的 org.springframework.web.context.ContextLoaderListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21237339/
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-13 07:32:23  来源:igfitidea点击:

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener in eclipse

javaeclipsespring-mvc

提问by Sudip7

I am doing a simple Spring MVC application(not using maven) which will print hello world on browser. It is a dynamic project in eclipse, so I put all the required jar files IN both build path as well as in WEB-INF/libfolder

我正在做一个简单的 Spring MVC 应用程序(不使用 maven),它将在浏览器上打印 hello world。它是 Eclipse 中的动态项目,因此我将所有必需的 jar 文件都放在构建路径和WEB-INF/lib文件夹中

I tried two solutions and both didn't work.

我尝试了两种解决方案,但都不起作用。

Solutions I found:

我发现的解决方案:

  1. cleaning tomcat work directory
  2. putting spring-mvc/spring-web.jar jar file in lib
  1. 清理tomcat工作目录
  2. 将 spring-mvc/spring-web.jar jar 文件放入 lib

Here is my web.xml

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

    <display-name>Spring MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

and there is no typo mistake in configuring the contextLoaderListener, then what is the problem, is there any other solution

并且配置contextLoaderListener没有错字,那是什么问题,有没有其他解决办法

Jan 20, 2014 8:16:39 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 20, 2014 8:16:39 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.11
Jan 20, 2014 8:16:40 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4618)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5184)
    at org.apache.catalina.core.StandardContext.call(StandardContext.java:5179)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Jan 20, 2014 8:16:40 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Jan 20, 2014 8:16:40 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Jan 20, 2014 8:16:40 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/SpringSample] startup failed due to previous errors
Jan 20, 2014 8:16:40 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["http-bio-8081"]
Jan 20, 2014 8:16:40 PM org.apache.coyote.AbstractProtocolHandler start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 20, 2014 8:16:40 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4010 ms

eventually I am getting 404 error as my service is failed to loaded

最终我收到 404 错误,因为我的服务无法加载

回答by Rida BENHAMMANE

Try to put the spring-web.jar file directly in the WEB-INF/lib (WEB-INF/lib/spring-web.jar) and not in WEB-INF/lib/spring-mvc/spring-web.jar

尽量把spring-web.jar文件直接放在WEB-INF/lib(WEB-INF/lib/spring-web.jar)而不是WEB-INF/lib/spring-mvc/spring-web.jar

回答by AllanT

I had this same problem in Eclipse. When I would do a maven clean install and move the generated WAR file to my Tomcat webapp folder it would run with no problems. However, inorder to debug I wanted to run in Eclipse but it would throw the following error.

我在 Eclipse 中遇到了同样的问题。当我执行 maven 全新安装并将生成的 WAR 文件移动到我的 Tomcat webapp 文件夹时,它会毫无问题地运行。但是,为了调试我想在 Eclipse 中运行但它会抛出以下错误。

SEVERE: Error configuring application listener of class   org.springframework.web.context.ContextLoaderListener

I googled around until I found the following blog post.

我四处搜索,直到找到以下博客文章。

http://commonexceptions.blogspot.com/2014/01/java.html

http://commonexceptions.blogspot.com/2014/01/java.html

As the author explains:

正如作者解释的那样:

This exception was caused when i did not add the maven dependencies to the build path. I was using eclipse with maven, so had to include the maven dependencies in the build path as explained below.

当我没有将 Maven 依赖项添加到构建路径时,会导致此异常。我使用 eclipse 和 maven,所以必须在构建路径中包含 maven 依赖项,如下所述。

See the post for the solution. It is a simple eclipse Deployment Assembly configuration fix. Took me about 30 seconds and now I can debug my Spring webapp in Eclipse.

解决方法见帖子。这是一个简单的 eclipse 部署程序集配置修复。我花了大约 30 秒,现在我可以在 Eclipse 中调试我的 Spring webapp。

Here are the relevant steps from the post:

以下是帖子中的相关步骤:

Adding Maven dependencies in project web deployment assembly :

在项目 web 部署程序集中添加 Maven 依赖项:

  • Open project properties(example: In project explorer rightClick on project name to select "Properties").
  • Select "Deployment Assembly".
  • Click "Add..." button on the right navigation.
  • Select "Java Build Path Entries" from menu of Directive Type and then click "Next".
  • Select "Maven Dependencies" from Java Build Path Entries menu
  • click "Finish".
  • Now the "Maven Dependencies" should be added to the Web Deployment Assembly and it should run.
  • 打开项目属性(例如:在项目资源管理器中右键单击项目名称以选择“属性”)。
  • 选择“部署程序集”。
  • 单击右侧导航中的“添加...”按钮。
  • 从指令类型菜单中选择“Java 构建路径条目”,然后单击“下一步”。
  • 从 Java Build Path Entries 菜单中选择“Maven Dependencies”
  • 单击“完成”。
  • 现在“Maven 依赖项”应该被添加到 Web 部署程序集中并且它应该运行。

When using Eclipse WDT and using unmodified and previously working project :

当使用 Eclipse WDT 并使用未修改且以前工作的项目时:

  • If Tomcat is running Stop it
  • Open the "Servers" view: Window then select "Show view" and then select "Other" > Server > Servers
  • Right Click on the Tomcat Server and then select "Clean Tomcat Work Directory".
  • Right Click on the Tomcat Server and then select "Clean"
  • Restart the Tomcat Server
  • 如果Tomcat正在运行停止它
  • 打开“服务器”视图:窗口然后选择“显示视图”,然后选择“其他”>服务器>服务器
  • 右键单击 Tomcat 服务器,然后选择“清理 Tomcat 工作目录”。
  • 右键单击Tomcat服务器,然后选择“清理”
  • 重启Tomcat服务器

The above solution should resolve the exception.

上述解决方案应该可以解决异常。

回答by atish shimpi

If you are creating simple project without maven, need to follow following steps,

如果你是在没有maven的情况下创建简单的项目,需要按照以下步骤,

  1. download required jar files, e.g. I am using spring 4 so I have downloaded files from Index of release

  2. Then add this files inside your WebContent/WEB-INF/libfolder.

  3. Add jar files in build path (In eclipse : Right click on project -> Properties -> Java Build Path -> Add JARs...)

  1. 下载所需的 jar 文件,例如我使用的是 spring 4,所以我已经从发布索引下载了文件

  2. 然后将此文件添加到您的WebContent/WEB-INF/lib文件夹中。

  3. 在构建路径中添加 jar 文件(在 eclipse 中:右键单击项目 -> 属性 -> Java 构建路径 -> 添加 JAR...)

This will avoid org.springframework.web.context.ContextLoaderListenerexception.

这将避免org.springframework.web.context.ContextLoaderListener异常。

回答by helpfulGuest

I have a VERY GOOD WAY to help you learn Spring MVC if you have Maven up and running.

如果您启动并运行了 Maven,我有一个非常好的方法可以帮助您学习 Spring MVC。

IF SO: go to your command line (Cygwin) I use...

如果是这样:转到您的命令行(Cygwin),我使用...

mvn archetype:generate It will ask for an 'archetype number'. For you... type 16 Enter the group ID which is just the main package. Enter Artifact ID which is your project name. SNAP-SHOT --- just press enter and same with version. Package - is the same as your group ID name. EX: com.spring Confirm it by entering the letter 'y' and press enter. DO all of the above after your are in your workspace directory. That way it is created there. You can do "mvn eclipse:eclipse" to load it in Eclipse OR you can just import it. I prefer the old fashioned importing an existing project.

mvn archetype:generate 它将要求一个“原型编号”。为您...输入 16 输入组 ID,它只是主包。输入 Artifact ID,即您的项目名称。SNAP-SHOT --- 只需按回车键,与版本相同。包 - 与您的组 ID 名称相同。EX: com.spring 通过输入字母“y”确认并按回车键。在您进入工作区目录后执行上述所有操作。这样它就在那里创建。您可以执行“mvn eclipse:eclipse”将其加载到 Eclipse 中,或者您可以直接导入它。我更喜欢老式的导入现有项目。

Everything will be 'already' set up for you in terms of ALL configuration (Java-Based) which is good for you. It will have all the Maven dependencies you need as well already in your pom.xml. You can add or take from it if you want.

就所有配置(基于 Java)而言,一切都将“已经”为您设置好,这对您有好处。它将在 pom.xml 中包含您需要的所有 Maven 依赖项。如果需要,您可以添加或从中获取。

The point here is that you will have a running project already and you can play with it from there. I create all my projects like this at first and erase what I don't need and add what I do and then go from there.

这里的重点是您已经有一个正在运行的项目,您可以从那里开始使用它。我首先像这样创建所有项目,然后删除我不需要的内容并添加我所做的然后从那里开始。

Good luck!!!

祝你好运!!!

回答by Giacinto Marcellino

It's a dependency problem, some dependencies are missing in the artifact. My solution in IntelliJ:

这是一个依赖问题,工件中缺少一些依赖。我在 IntelliJ 中的解决方案:

Right Click on project name ->open module settings -> problems -> add missing dependencies to the artifact

右键单击项目名称 -> 打开模块设置 -> 问题 -> 向工件添加缺少的依赖项

Restart the server (my case was tomcat)

重启服务器(我的情况是tomcat)

Hope it helps

希望能帮助到你