Spring boot: Unable to start embedded Tomcat servlet container
Asked Answered
A

13

47

I'm new to Spring Boot and having with error while running my application. I'm following a tutorial and I believe I'm having proper parent and dependencies with POM, please help me

main class:

package com.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Hello world!
 *
 */

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, "hello");
    }
}

error is:

   org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat servlet container
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:165) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:293) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        at com.boot.App.main(App.java:18) [classes/:na]Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:159) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]
        ... 10 common frames omitted

POM :

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>

  <groupId>com.boot</groupId>
  <artifactId>das-boot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>
  <name>das-boot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Antabuse answered 24/4, 2017 at 3:41 Comment(1)
Post the entire stack trace. There's an additional line in there directly from Tomcat, probably saying that you already have something open on the port.Fragmentary
I
42

Try to change the port number in application.yaml (or application.properties) to something else.

Illuminance answered 24/4, 2017 at 7:59 Comment(3)
I can't find this directory which IDE are you referring to? However when I set this port programatically it's working although I have to change the port on every run .. please suggest how to fix this. System.getProperties().put( "server.port", 130 );Antabuse
Check this out: github.com/khoubyari/spring-boot-rest-example/blob/master/src/…Illuminance
..although my application did not have this configuration file. The TCP port conflict was the problem, so this post was still very helpful. The key errors were: Failed to start end point associated with ProtocolHandler and java.net.BindException: Address already in useAegyptus
B
15

You need to add the tomcat dependency in your pom

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Befoul answered 24/4, 2017 at 10:1 Comment(1)
I have this dependency, still getting the exception org.springframework.boot.web.server.WebServerException: Unable to start embedded TomcatPare
H
6

In my case, I had multiple errors where the first one was: ,,Unable to start embedded Tomcat servlet container". I tried to solve the first one multiple times, but the real problem was in the rest of them. Turns out I had two different imports of the Logger, so when I tried to initialize it there wasn't a compile problem but it was a runtime error.

My advice: check all the error you have, maybe the problem is not in the first one as I initially thought. So maybe solution to this problem is in the next displayed error.

If this is the only error you have, the solution is probably on of the above answers.

Hsining answered 3/4, 2022 at 22:33 Comment(0)
D
5

If you are running on a linux environment, basically your app does not have rights for the default port.

Try 8181 by giving the following option on VM.

-Dserver.port=8181

Duncan answered 9/9, 2020 at 8:55 Comment(0)
A
2

In my condition when I got an exception " Unable to start embedded Tomcat servlet container",

I opened the debug mode of spring boot by adding debug=true in the application.properties,

and then rerun the code ,and it told me that java.lang.NoSuchMethodError: javax.servlet.ServletContext.getVirtualServerName()Ljava/lang/String

Thus, we know that probably I'm using a servlet API of lower version, and it conflicts with spring boot version.

I went to my pom.xml, and found one of my dependencies is using servlet2.5, and I excluded it.

Now it works. Hope it helps.

Alarcon answered 9/3, 2018 at 3:54 Comment(0)
N
2

For me, I just had to put a -X flag with mvn. Look at the debug log; there was an issue with Spring while locating the .properties file.

Niece answered 6/9, 2018 at 5:56 Comment(0)
L
2

This could happen due to the change in java version of the project.Say for example if the project is build in java 8 and if we change the java version to 11 then there can be such issue. In intellij idea go to the File->Project Structure then change the Project SDK Version.

Leadership answered 12/2, 2021 at 7:22 Comment(0)
B
0

You need to Tomcat Dependency and also extend your Application Class from extends SpringBootServletInitializer

@SpringBootApplication  
public class App extend SpringBootServletInitializer
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, "hello");
    }
}
Bobbysoxer answered 30/11, 2017 at 8:23 Comment(0)
L
0

Simple way to handle this is to include this in your application.properties or .yml file: server.port=0 for application.properties and server.port: 0 for application.yml files. Of course need to be aware these may change depending on the springboot version you are using. These will allow your machine to dynamically allocate any free port available for use. To statically assign a port change the above to server.port = someportnumber. If running unix based OS you may want to check for zombie activities on the port in question and if possible kill it using fuser -k {theport}/tcp. Your .yml or .properties should look like this. server: port: 8089 servlet: context-path: /somecontextpath

Limit answered 15/9, 2018 at 21:44 Comment(0)
P
0

For me, the problem was in XML migrations. I deleted all tables and sequences and it works on next bootRun

Pepe answered 21/6, 2020 at 8:57 Comment(0)
W
0

As said above, if you are running on a Linux, your app does not have access to 80 port. There are two ways to solve this issue:

  1. Run your application with root previlegies - good idea only for local testing, not for any production.

  2. Run app at port 8081 for example, setup reverse-proxy with nginx, and redirect requests from 80 to 8081.

Winze answered 9/9, 2021 at 8:45 Comment(0)
S
0

It seems that you might have used Mapping incorrectly one of your, if not more, entities. Fix that. And the error should go away.

In my case, I forgot that I mapped two entities and left one of them with an object annotated with @OneToMany. I commented out and I am now rid of that error.

Smokestack answered 21/2, 2023 at 6:36 Comment(0)
S
0

Though may not be OP's solution, my search brought me here, and then I figured out what my issue was causing this same error message.

My application-beta.yml was named properly to match my run configuration > active profile setting (my IDE = intelliJ). However, I missed one spot in the yml file where a property needed to be set:

spring:
  profiles: betarclocal

needed to be changed to match the "beta" active profile:

spring:
  profiles: beta

Simply overlooked this and all the properties in the yml config under this were being ignored... And one of the properties were needed for the tomcat server.

Long story shory: make sure your active profiles, spring profiles, and application-[activeprofile].yml are all named uniformly.

Steradian answered 12/9, 2023 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.