Skip to content

How to resolve 'Port 8080 was already in use' problem in SpringBoot apps

Problem

When developing Spring Boot RESTful service and client (consumer) applications, the architecture is as follows:

  • Spring Boot RESTful service: A web service that listens on port 8080.
  • Spring Boot RESTful client (consumer): A RESTful client that consumes the web service.

image-20201120140146339

When starting both applications with gradle bootRun, the following problem occurs:

> Task :app:bootRun FAILED
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.2.RELEASE)
2020-11-18 18:01:35.477 INFO 5494 --- [ main] com.bswen.app1.ConsumingRestApplication : Starting ConsumingRestApplication on MacBook-Pro-bswen.local with PID 5494 (/Users/bswen/bswen-springboot23/app1/build/classes/java/main started by bswen in /Users/bswen/bswen-springboot23/app1)
2020-11-18 18:01:35.479 INFO 5494 --- [ main] com.bswen.app1.ConsumingRestApplication : No active profile set, falling back to default profiles: default
2020-11-18 18:01:36.088 INFO 5494 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-11-18 18:01:36.095 INFO 5494 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-11-18 18:01:36.096 INFO 5494 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-11-18 18:01:36.139 INFO 5494 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-11-18 18:01:36.139 INFO 5494 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 623 ms
2020-11-18 18:01:36.279 INFO 5494 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-18 18:01:36.381 WARN 5494 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
2020-11-18 18:01:36.382 INFO 5494 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-11-18 18:01:36.384 INFO 5494 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-11-18 18:01:36.393 INFO 5494 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-18 18:01:36.400 ERROR 5494 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
Execution failed for task ':app1:bootRun'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

Environment

  • Gradle: 6.2.x
  • Spring Boot: 2.3
  • IntelliJ IDEA: 2019.3

Reason

The RESTful client is also a Spring Boot web application, which listens on port 8080 by default. This causes a port conflict.

image-20201120141030468

Solution

The key point is to disable the web server in the Spring Boot RESTful client application. You can do this as follows:

  1. Open the src/main/resources/application.yml or application.properties file.

  2. If you are using application.yml, add these lines:

    application.yml
    spring:
    main:
    web-application-type: NONE
  3. If you are using application.properties, add these lines:

    application.properties
    spring.main.web-application-type=NONE

Restart the applications, and everything should work correctly.

All the code is shared on GitHub. You can access it here.

Summary

In this post, we explored how to resolve the “Port 8080 was already in use” error in Spring Boot applications. The issue arises when both the RESTful service and client applications attempt to use the same port. By disabling the web server in the client application, you can avoid port conflicts and ensure both applications run smoothly. This solution is particularly useful when developing microservices or client-server architectures in Spring Boot.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!