Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to initialize component caused by factory already defined

Tags:

spring-boot

Created spring boot standalone application which was working fine when running in Spring Tool Suite 4. When trying to run the jar(build using export jar) from command line observed error as:

{
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

}
like image 586
sri Avatar asked Sep 03 '25 10:09

sri


1 Answers

I faced the very same issue. After doing a lot of research it seemed to me that the easiest solution is to run your app on Jetty instead of Tomcat9.

Apparently the URLStreamHandlerFactory can only be set once and sometimes something other than your app beat it to the punch, for instance in Java 9. So then when tomcat tries to set the factory for your app it has already been done and it crashes. I did not have this problem on my old Windows 7 computer but I do on my Windows 10.

I found some suggestions to force a set of the factory etc, but none of the suggestions seemed optimal. I solved it by replacing Tomcat with Jetty as such in my pom.xml file:

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

You should not need to change anything else, unless you have made custom settings to your tomcat servlet, but of course you should clean and rebuild your project to update it.

like image 115
Vanheden Avatar answered Sep 05 '25 02:09

Vanheden