44

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>
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
amar
  • 451
  • 1
  • 5
  • 9

12 Answers12

41

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

Aziz
  • 931
  • 2
  • 10
  • 19
  • 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 );` – amar Apr 24 '17 at 14:51
  • Check this out: https://github.com/khoubyari/spring-boot-rest-example/blob/master/src/main/resources/application.yml – Aziz Apr 24 '17 at 22:12
  • ..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 use – Josiah DeWitt Nov 22 '19 at 18:43
14

You need to add the tomcat dependency in your pom

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
cralfaro
  • 5,822
  • 3
  • 20
  • 30
  • 2
    I have this dependency, still getting the exception org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat – Pablo Pazos May 12 '20 at 19:59
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

Kıvılcım
  • 391
  • 3
  • 8
4

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.

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.

Norman Bai
  • 145
  • 6
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.

Jawad Khan
  • 156
  • 1
  • 8
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.

Senthuran
  • 1,583
  • 2
  • 15
  • 19
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");
    }
}
CGS
  • 2,782
  • 18
  • 25
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

Amos Kosgei
  • 877
  • 8
  • 14
0

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

Andrej
  • 196
  • 2
  • 9
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.

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.

Sendi
  • 11
  • 2