Spring Boot is a powerful tool for building Spring-powered standalone and web applications in Java. It provides out-of-the-box defaults for its configuration properties, but also allows us to override the default properties if required.

In this quick article, you'll learn how to change the default port in Spring Boot. By default, the embedded web server uses port 8080 to start the Spring Boot application. There are several ways available to change that port as explained below.

Using Configuration Files

The easiest and faster way to change the default port in Spring Boot is by overriding the default value in a configuration file. Spring Boot uses server.port configuration property to specify the port.

The following example shows how you can specify a custom port in application.properties file:


Now the server will launch the application on port 8888. For application.yml, you need to add the following:

    port: 8888

Spring Boot automatically loads the above files if placed in the src/main/resources/ folder.

Using System Property

You can also set a system property to change the default port of the Spring Boot application. All you need to do is just set an environment variable called SERVER_PORT on the operating system where the server will be started.

For UNIX-based operating system, type the following command to set an environment variable:

export SERVER_PORT=8888

For Windows operating system, you have to use the following command:

setx SERVER_PORT 8888

Using Command-Line Arguments

Another way to change the default port in Spring Boot is by using the command-line arguments while launching the application. For example, if you are packaging and running the application as a jar file, you can set the server.port argument with the Java command:

$ java -jar spring-boot-app.jar --server.port=8888

The above command is equivalent to the following:

$ java -jar -Dserver.port=8888 spring-boot-app.jar

Programmatic Configuration

You can also change the default port programmatically either while starting the application or by customizing the embedded server configuration.

To set the port property in main application class while starting the application, do the following:

public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setDefaultProperties(Collections.singletonMap("server.port", "8888"));


To customize the embedded server configuration, you have to implement the WebServerFactoryCustomizer interface as shown below:

public class PropertiesCustomizer
        implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

    public void customize(ConfigurableWebServerFactory factory) {

Environment-Specific Ports

If you are already using Spring Boot profiles to specify configuration properties for different environments, you may want to run the application on different ports.

Let us say you want to configure different ports for development and production environments. To achieve this, create a new file called application-dev.properties for the development environment in the src/main/resources/ folder with the following content:


Next, create a application-prod.properties file for the production environment with a different port:


To activate the desired Spring Boot profile, add the following property to application.properties file:

# spring boot profile - div or prod

That's it. Spring Boot will automatically pick the server port for the currently active profile.

Using Random Port

If you want to run the Spring Boot application on any available random port, just set the server.port=0 property. The embedded web server will look for a free port using OS natives to prevent clashes and assign it to the application.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Subscribe to RSS Feed.

👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5).