springboot-Two ways to solve address already in use with SpringBoot apps

1. Introduction

Sometimes, when we start SpringBoot web(mvc) application, we got this exception:

2019-06-24 13:41:59.855 ERROR 68431 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[HTTP/1.1-8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:167) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.apache.catalina.core.StandardService.addConnector(StandardService.java:225) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.addPreviouslyRemovedConnectors(TomcatWebServer.java:256) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:198) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.2.RELEASE.jar:2.0.2.RELEASE]
  at com.bswen.sbmvc.Application.main(Application.java:10) [classes/:na]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
  at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
Caused by: org.apache.catalina.LifecycleException: Protocol handler start failed
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:1020) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  ... 18 common frames omitted
Caused by: java.net.BindException: Address already in use
  at sun.nio.ch.Net.bind0(Native Method) ~[na:1.8.0_121]
  at sun.nio.ch.Net.bind(Net.java:433) ~[na:1.8.0_121]
  at sun.nio.ch.Net.bind(Net.java:425) ~[na:1.8.0_121]
  at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[na:1.8.0_121]
  at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) ~[na:1.8.0_121]
  at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:210) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:1150) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:591) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  at org.apache.catalina.connector.Connector.startInternal(Connector.java:1018) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
  ... 19 common frames omitted
2019-06-24 13:41:59.862  INFO 68431 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-06-24 13:41:59.875  INFO 68431 --- [           main] ConditionEvaluationReportLoggingListener : 
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-24 13:41:59.877 ERROR 68431 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
***************************
APPLICATION FAILED TO START
***************************
Description:
The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.

2. Environments

  • SpringBoot 1.x and 2.x

3. The Solution

3.1 Way 1: Change your port

This exception means that your port has been already used by other apps. So the simplest way to solve this issue is to change your port like this:

Add this line to your src/main/resources/application.properties

server.port=28080

3.2 Way 2: Stop or kill the old app

If you just want to use the port 8080, and you can do as follows to check what app use that port like this:

➜  bswen-project git:(master) yum install lsof
➜  bswen-project git:(master) lsof -i:8080
COMMAND   PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
java    68428 bswen.com   75u  IPv6 0x2b29170d4b2d39b7      0t0  TCP *:http-alt (LISTEN)
➜  bswen-project git:(master)
➜  bswen-project git:(master) jps -l -m|grep 68428
68428 com.intellij.rt.execution.application.AppMain springboot2.mvc.Application

Then you can find that the process’ pid is 68428,just kill it like this:

➜  bswen-project git:(master) kill -9 68428

Then everything would be ok.

4. Summary

You can avoid to use the default 8080 port of springboot apps by using specific property , or use the lsof and jps to determine the old app and kill it.