创建的Spring Boot独立应用程序在Spring Tool Suite 4中运行时运行良好。当尝试从命令行运行jar(使用导出jar进行构建)时,观察到错误为:
{
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[jar:rsrc:spring-context-5.1.2.RELEASE.jar!/:na]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at games.hungama.songbeats.SongBeats1Application.main(SongBeats1Application.java:10) [rsrc:./:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61) [SongBeats.jar:na]
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:414) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
... 13 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:960) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:261) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.startup.Tomcat.start(Tomcat.java:370) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:106) ~[jar:rsrc:spring-boot-2.1.0.RELEASE.jar!/:na]
... 18 common frames omitted
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during start
at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.8.0_161]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[na:1.8.0_161]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:951) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
... 26 common frames omitted
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:960) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_161]
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) ~[na:1.8.0_161]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
... 26 common frames omitted
Caused by: java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.StandardRoot@7dfdd352]
at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.8.0_161]
at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[na:1.8.0_161]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:951) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
... 34 common frames omitted
Caused by: org.apache.catalina.LifecycleException: Failed to initialize component [org.apache.catalina.webresources.StandardRoot@7dfdd352]
at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:441) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:139) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:173) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4758) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4893) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_161]
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134) ~[na:1.8.0_161]
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
... 34 common frames omitted
Caused by: java.lang.Error: factory already defined
at java.net.URL.setURLStreamHandlerFactory(URL.java:1112) ~[na:1.8.0_161]
at org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.<init>(TomcatURLStreamHandlerFactory.java:130) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.getInstanceInternal(TomcatURLStreamHandlerFactory.java:53) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.webresources.TomcatURLStreamHandlerFactory.register(TomcatURLStreamHandlerFactory.java:77) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.webresources.StandardRoot.registerURLStreamHandlerFactory(StandardRoot.java:699) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.webresources.StandardRoot.initInternal(StandardRoot.java:682) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:136) ~[jar:rsrc:tomcat-embed-core-9.0.12.jar!/:na]
... 44 common frames omitted
}
答案 0 :(得分:1)
我面临着同样的问题。经过大量研究,在我看来,最简单的解决方案是在Jetty上运行您的应用程序,而不是Tomcat9。
很明显,URLStreamHandlerFactory只能设置一次,有时除了您的应用程序以外,其他应用程序都无法胜任,例如在Java 9中。因此,当tomcat尝试为您的应用程序设置工厂时,它已经完成并且崩溃了。我的旧Windows 7计算机上没有这个问题,但我的Windows 10上却有。
我发现了一些建议来强制建立一套工厂等,但是这些建议似乎都不是最优的。我通过在pom.xml文件中将Tomcat替换为Jetty来解决了这个问题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
您不需要更改任何其他内容,但是您当然应该清理并重建项目以进行更新。