无法启动Spring-Boot示例

时间:2013-09-20 05:24:50

标签: java spring maven spring-boot

我正在尝试弹簧靴样品。这是我一直试图运行的示例spring-boot-sample-tomcat

按照说明,我已经运行mvn package并且成功了。 但是当运行jar java -jar target/spring-boot-sample-tomcat-0.5.0.BUILD-SNAPSHOT.jar时,我得到以下异常。

请确认是否有任何需要放入类路径的罐子。 或者如何使其运行。

链接到正在使用的pom.xml

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
        at java.lang.Class.getDeclaredMethod(Class.java:1935)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at org.springframework.boot.loader.LaunchedURLClassLoader.findClass(LaunchedURLClassLoader.java:110)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at org.springframework.boot.loader.LaunchedURLClassLoader.doLoadClass(LaunchedURLClassLoader.java:93)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:65)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 5 more

3 个答案:

答案 0 :(得分:4)

您可以尝试将此细分添加到您的pom:

<properties>
  <start-class>org.springframework.boot.sample.tomcat.SampleTomcatApplication</start-class>
</properties>

这定义了显式使用的“main”。 Spring boot应该自动获取main,但显式是一个很好的方法。这是我能够使用“java -jar”调用工作来获得另一个项目的唯一方法。

答案 1 :(得分:1)

我刚刚执行了:

$ git fetch upstream
$ git merge upstream/master
Already up-to-date.
$ cd spring-boot-samples/spring-boot-sample-tomcat/
$ mvn clean package && java -jar target/spring-boot-sample-tomcat-0.5.0.BUILD-SNAPSHOT.jar
...
Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
...
2013-09-23 08:52:33.713  INFO 698 --- [           main] o.s.b.s.tomcat.SampleTomcatApplication   : Starting SampleTomcatApplication on retina with PID 698 (/Users/gturnquist/src/spring-boot/spring-boot-samples/spring-boot-sample-tomcat/target/spring-boot-sample-tomcat-0.5.0.BUILD-SNAPSHOT.jar started by gturnquist)
2013-09-23 08:52:33.753  INFO 698 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21301fa: startup date [Mon Sep 23 08:52:33 CDT 2013]; root of context hierarchy
2013-09-23 08:52:34.673  INFO 698 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2013-09-23 08:52:34.674  INFO 698 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.42
2013-09-23 08:52:34.753  INFO 698 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2013-09-23 08:52:34.754  INFO 698 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1004 ms
2013-09-23 08:52:34.902  INFO 698 --- [ost-startStop-1] o.s.b.s.tomcat.SampleTomcatApplication   : ServletContext initialized
2013-09-23 08:52:34.918  INFO 698 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2013-09-23 08:52:34.919  INFO 698 --- [ost-startStop-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2013-09-23 08:52:34.991  INFO 698 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2013-09-23 08:52:35.127  INFO 698 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.springframework.boot.sample.tomcat.web.SampleController.helloWorld()
2013-09-23 08:52:35.169  INFO 698 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2013-09-23 08:52:35.169  INFO 698 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2013-09-23 08:52:35.377  INFO 698 --- [ost-startStop-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 458 ms
2013-09-23 08:52:37.548  INFO 698 --- [           main] o.s.b.s.tomcat.SampleTomcatApplication   : Started SampleTomcatApplication in 4.299 seconds

也许您需要更新克隆并重建?

答案 2 :(得分:0)

我遇到了同样的问题,我的问题是我在属性的start-class上配置了错误的类,一旦删除它,一切都很好:

<properties>
  <start-class>wrong class here</start-class>  <-- REMOVE THIS LINE
</properties>