Java 无法启动 Spring-Boot 示例

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

Not able to start the Spring-Boot sample

javaspringmavenspring-boot

提问by Kunal Jha

I was trying out spring-boot samples. This is specifically the example spring-boot-sample-tomcat, that I have been trying to run.

我正在尝试 spring-boot 示例。这是我一直在尝试运行的示例spring-boot-sample-tomcat

As instructed, I have run mvn packageand it is successful. But when running the jar java -jar target/spring-boot-sample-tomcat-0.5.0.BUILD-SNAPSHOT.jar, I am getting the below exception.

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

Please confirm if there is any jars which need to be put in the classpath. Or what to do to make it run.

请确认是否有任何需要放入类路径中的 jars。或者怎么做才能让它运行。

Link to the pom.xmlwhich is being used.

链接到正在使用的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.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

采纳答案by sanimalp

You might try adding this segment to your pom:

您可以尝试将此段添加到您的 pom 中:

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

This defines the "main" to use explicitly. Spring boot is supposed to pick up the main automatically, but being explicit is a good way to go. That was the only way I was able to get another project I was working on to work using the "java -jar" invocation working.

这定义了要显式使用的“主要”。Spring boot 应该自动选择 main,但明确是一个很好的方法。这是我能够使用“java -jar”调用使我正在处理的另一个项目工作的唯一方法。

回答by gregturn

I just executed:

我刚刚执行:

$ 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

Perhaps you need to update your clone and rebuild?

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

回答by user3584001

I had the same issue and my problem was that I had the wrong class configured on start-class under properties, once I removed it, everything was fine:

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

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