How to Manage the Embedded Apache Tomcat Instance in a Spring Boot Application?

Question

How can I manage the embedded instance of Apache Tomcat in my Spring Boot application?

// application.properties
server.port=8090

Answer

When developing a Spring Boot application, you might encounter issues with the embedded Apache Tomcat server, particularly when trying to change the port it's listening on. This can happen if the previous instance of the server is still running, or if there's an existing process utilizing that port. Below, we will explore how to configure your application properties and reliably manage your embedded Tomcat instance.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory();
    }
}

Causes

  • The specified port (e.g., 8090) is already in use by another application or another instance of the same application.
  • The application has not been properly stopped from a previous run, causing the port to remain busy.
  • Incorrect configuration in the application.properties file may lead to connectivity issues.

Solutions

  • Check for running processes on the specified port and stop those processes if necessary.
  • Ensure that your application is completely stopped before restarting it, which can be done by properly shutting down the application from your IDE or terminal.
  • Modify the application to check for the availability of the specified port before attempting to bind to it. This could involve wrapping the server startup in a try-catch block and handling exceptions appropriately.

Common Mistakes

Mistake: Not stopping the application from the IDE before a subsequent run.

Solution: Always ensure you stop the running application before attempting to restart it.

Mistake: Trying to bind to a port that is still being used by another service.

Solution: Use `netstat -ano | findstr :8090` (on Windows) or `lsof -i :8090` (on Mac/Linux) to find and stop the process occupying the port.

Helpers

  • Spring Boot
  • Embedded Tomcat
  • Server Port Configuration
  • Spring Framework
  • REST API Development
  • Tomcat Connector Error
  • Port Already in Use
  • Application Configuration

Related Questions

⦿How to Handle Hibernate AnnotationException: No Identifier Specified For Entity Without Primary Key

Learn how to resolve Hibernates AnnotationException when your database table lacks a primary key. Discover best practices and solutions without altering the table.

⦿How to Sort an Array of Objects by a Property in Java?

Learn how to sort an array of objects in Java by a specific property like name including code examples and common mistakes.

⦿How to Use the `@Value` Annotation in Java Spring for Property Injection?

Learn how to efficiently inject environment properties in Java Spring using the Value annotation with concise examples.

⦿How to Fill Java Arrays with Ranges of Numbers Like in Perl?

Learn how to effectively fill Java arrays with numeric ranges and explore packages for accessing nth elements without array creation.

⦿How to Create a Regular Expression to Accept Only Alphanumeric Characters

Learn how to craft a regex that allows only alphanumeric characters with detailed examples and common pitfalls.

⦿How to Remove Trailing Zeros from a String in Java?

Learn how to efficiently remove trailing zeros from numeric strings in Java using regex. Solutions and code examples included.

⦿How to Resolve IOException with File.createNewFile() Method in Java?

Learn how to fix IOException when using File.createNewFile in Java with code examples and common troubleshooting tips.

⦿How to Convert a Long Timestamp to a Byte Array and Insert It Efficiently

Learn how to convert a long timestamp to a byte array and efficiently insert it into an existing byte array without bitwise operations.

⦿How to Generate a Random Integer Between a Minimum and Maximum Value in Java?

Learn how to generate a random integer between specified min and max values in Java with code examples and troubleshooting tips.

⦿How to Resolve "Java Cannot Access Class: Class File Not Found" Error in IntelliJ

Learn how to fix the cannot access javax.xml.bind.RootElement error in IntelliJ due to missing class files for your Java project.

© Copyright 2025 - CodingTechRoom.com