How to configure port for a Spring Boot application

Paul Verest Source

How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.



answered 5 years ago Paul Verest #1

As said in docs either set server.port as system property using command line option to jvm --server.port=8090 or add in /src/main/resources/ with

server.port = 8090

For random port use


answered 5 years ago nndru #2

You can specify port by overriding EmbeddedServletContainerFactory bean within your configuration (java based or xml). There you can specify port for used embedded servlet container. Please, see Spring Boot - Core "Embedded Servlet Container Support" paragraph and example there. Hope this helps.

answered 4 years ago gatolgaj #3

In case you are using application.yml add the Following lines to it

     port: 9000

and of course 0 for random port.

answered 3 years ago anataliocs #4

There are two main ways to change the port in the Embedded Tomcat in a Spring Boot Application.


First you can try the file in the /resources folder: file

Modify a VM option

The second way, if you want to avoid modifying any files and checking in something that you only need on your local, you can use a vm arg:

Go to Run -> Edit Configurations -> VM options


Change port with a vm arg

Additionally, if you need more information you can view the following blog post here: Changing the port on a Spring Boot Application

answered 3 years ago Rakesh #5

  1. As everyone said, you can specify in
    server.port = 9000 (could be any other value)

  2. If you are using spring actuator in your project, by default it points to
    8080, and if you want to change it, then in mention
    management.port = 9001 (could be any other value)

answered 3 years ago makerj #6

also, you can configure port programmatically

public class ServletConfig {
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {

answered 3 years ago Adrian Cosma #7

Indeed, the easiest way is to set the server.port property.

If you are using STS as IDE, from version 3.6.7 you actually have Spring Properties Editor for opening the properties file.

This editor provides autocomplete for all Spring Boot properties. If you write port and hit CTRL + SPACE, server.port will be the first option.

answered 3 years ago opensourcegeek #8

You can add the port in below methods.

  1. Run -> Configurations section

  2. In application.xml add server.port=XXXX

answered 3 years ago ayurchuk #9

You can set port in java code:

HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);

new SpringApplicationBuilder()

Or in application.yml:

    port: 9999

Or in


Or as a command line parameter:


answered 3 years ago Luis Mauricio #10

When you need a programatically way of doing it, you can set it during startup:

System.getProperties().put( "server.port", 80 );, args);

This might help for things like environment dependent port. Have a nice day

answered 3 years ago itwarilal #11

If you would like to run it locally, use this -

mvn spring-boot:run -Drun.jvmArguments='-Dserver.port=8085'

As of Spring Boot 2.0, here's the command that works (clues were here):

mvn spring-boot:run

answered 2 years ago Mercurial #12

In my case adding statement


override the default tomcat server port.

answered 2 years ago Ali Dehghani #13

You can also use SERVER_PORT environment variable to configure Spring Boot port. Just set the environment variable and restart the app:

set SERVER_PORT=9999 // on windows machine
export SERVER_PORT=9999 // on linux

Please note that if you do not set those environment variables system wide, you should run the boot app on the same session.

answered 2 years ago Abhijeet #14

You can set that in under /src/main/resources/

server.port = 8090

answered 2 years ago Sridhar Battala #15

Add this in your file

server.port= 8080

answered 2 years ago Ulises #16

Using property server.port=8080 for instance like mentioned in other answers is definitely a way to go. Just wanted to mention that you could also expose an environment property:


Since spring boot is able to replace "." for "_" and lower to UPPER case for environment variables in recent versions. This is specially useful in containers where all you gotta do is define that environment variable without adding/editing or passing system properties through command line (i.e -Dserver.port=$PORT)

answered 2 years ago mujeeb rahman #17

"server.port=8080" will only works if your running application as a jar through main method,

This configuration will not work if your running this application as a war file through tomcat container.

answered 2 years ago Chandramouli #18

Include below property in


answered 2 years ago Rong.l #19

If you are going to run apps as jar file in command environment, just type "SERVER_PORT=*** " as prefix. The full command to execute will look like below:

SERVER_PORT=8080 java -jar ***.jar

If you wanna run app in background in Linux, command with 'nohup' will look like below:

SERVER_PORT=8080 nohup java -jar ***.jar &

answered 2 years ago tan9 #20

Since Spring Boot provides various configuration externalization mechanism (through various PropertySource implementations and/or processors wired into Environment object in order), you can set any property outside of your jar archive through following methods:

  1. Pass property through command line argument as application argument

    java -jar <path/to/my/jar> --server.port=7788
  2. From property in SPRING_APPLICATION_JSON (Spring Boot 1.3.0+)

    • Define environment variable in U*IX shell:

      SPRING_APPLICATION_JSON='{"server.port":7788}' java -jar <path/to/my/jar>
    • By using Java system property:

      java -Dspring.application.json='{"server.port":7788}' -jar <path/to/my/jar>
    • Pass through command line argument:

      java -jar <path/to/my/jar> --spring.application.json='{"server.port":7788}'
  3. Define JVM system property

    java -Dserver.port=7788 -jar <path/to/my/jar>
  4. Define OS environment variable

    • U*IX Shell

      SERVER_PORT=7788 java -jar <path/to/my/jar>
    • Windows

      SET SERVER_PORT=7788
      java -jar <path/to/my/jar>
  5. Place property in ./config/ configuration file


    and run:

     java -jar <path/to/my/jar>
  6. Place property in ./config/application.yaml

        port: 7788

    and run:

     java -jar <path/to/my/jar>
  7. Place property in ./


    and run:

     java -jar <path/to/my/jar>
  8. Place property in ./application.yaml

        port: 7788

    and run:

     java -jar <path/to/my/jar>

You can combine above methods all together, and the former configuration in the list take precedence over the latter one.

For example:

SERVER_PORT=2266 java -Dserver.port=5566 -jar <path/to/my/jar> --server.port=7788

The server will start and listen on port 7788.

This is very useful providing default properties in PropertySources with lower precedence (and usually packaged in the archive or coded in the source), and then override it in the runtime environment. And it is the design philosophy of Spring Boot:

Be opinionated out of the box, but get out of the way quickly as requirements start to diverge from the defaults.

SERVER_NAME to conversion was done by Relaxed Binding.

answered 2 years ago OlivierTerrien #21

As explained in Spring documentation, there are several ways to do that:

Either you set the port in the command line (for example 8888)

-Dserver.port=8888 or --server.port=8888

Example : java -jar -Dserver.port=8888 test.jar

Or you set the port in the


or (in application.yml with yaml syntax)

   port: ${port:4588}

If the port passed by -Dport (or -Dserver.port) is set in command line then this port will be taken into account. If not, then the port will be 4588 by default.

If you want to enforce the port in properties file whatever the environment variable, you just have to write:


answered 2 years ago Lova Chittumuri #22

The default port is : 8080 but we can customize the port number in as shown below

server.port = 5050 -- #here we can give any port number.

answered 2 years ago Automator1992 #23

You can configure your port in file in the resources folder of your spring boot project.

server.port="port which you need"

answered 1 year ago Amit Gujarathi #24

In file present in resources:


answered 1 year ago Amandeep Singh #25

There are three ways to do it

1 Set server.port property in file

server.port = 8090

2 Set server port property in application.yml file

     port: 8090

3 Set the property as system property in main method


answered 1 year ago Pau #26

To extend other answers:

There is a section in the docs for testing which explains how to configure the port on integration tests:

At integration tests, the port configuration is made using the annotation @SpringBootTest and the webEnvironment values.

Random port:


You can inject the value using @LocalServerPort which is the same as @Value("${local.server.port}").

  • Example:

Random port test configuration:

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ExampleTest {
   @LocalServerPort //to inject port value
   int port;

Defined port:


It takes the value from server.port if is defined.

  • If is defined using @TestPropertySource(properties = "server.port=9192"), it overrides other defined values.
  • If not, it takes the value from src/test/resources/ (if exists).
  • And finally, if it is not defined it starts with the default 8080.


Defined port test configuration:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=9192")
public class DemoApplicationTests {

    public void contextLoads() {


answered 1 year ago Shubham Khurana #27

Just have a in src/main/resources of the project and give there


where **** refers to the port number.

answered 1 year ago S'chn T'gai Spock #28

This worked for me :

Added a custom container class :

public class CustomContainer implements EmbeddedServletContainerCustomizer {

    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {


But this was still not using port 8888.

Then I set "scanBasePackages" property like this on "@SpringBootApplication" class on my main method: (scanBasePackages = {"custom container package"})

@SpringBootApplication(scanBasePackages = {"com.javabrains.util"})
public class CourseApiApp {

    public static void main (String args []) {, args);

And it started picking up port set in Custom Container.

answered 1 year ago Mohanraj #29

Mostly springboot runs on port:8080 because of embedded Tomcat. In some it may throw an error port 8080 already in use. To avoid this kind of issues we can config the server port.


add server.port=9898

On runtime config

run your application with below arguments.

spring-boot:run -Drun.jvmArguments='-Dserver.port=8081'

answered 1 year ago Buddhika Lakshan #30

1.1 Update via a properties file.



Update via a yaml file.


     port: 8888


public class CustomContainer implements EmbeddedServletContainerCustomizer {

    public void customize(ConfigurableEmbeddedServletContainer container) {




answered 1 year ago Anushka Ekanayake #31

In spring boot you can easily configure the service exposing port in file.


if you don't set a port specifically, then it will try to open the service with port 80. in case if it is already used, service will not be started on the embedded container.

answered 1 year ago Humoyun #32

When spring boot application starts, the embedded server such as Tomcat starts with a default port. The embedded tomcat starts with 8080 port as default. There are many ways to change default server port.

Using Property File (.properties/.yml)

To change server port using property file, we need to configure server.port property.

a. Using in classpath such as src\main\resources\

server.port = 8585

The server will start with 8585 port. To get random server port, assign 0 to the property.

server.port = 0

Now spring boot will start the server on a port that is not being used currently by any server in the system.

b. Using application.yml in classpath such as src\main\resources\application.yml.

  port: 8585 

Server will start with 8585 port.

For random port, assign 0.

  port: 0 

Using java Command with --server.port or -Dserver.port

Suppose we have an executable JAR named as my-app.jar, then while starting spring boot application using java command we can use the argument as follows.

Using --server.port

java -jar my-app.jar  --server.port=8585

Using -Dserver.port

java -jar -Dserver.port=8585 my-app.jar

Server will start with 8585 port.

Using java Command with --port or -Dport in Short

To make --server.port and -Dserver.port in short, we can remove server keyword and make it any short keyword such as --port and -Dport. We can use any short keyword. Here we are using port as short keyword. To achieve it we need to configure placeholder in property file as follows.



Using application.yml

   port: ${port:8282}

If we do not pass the port as the argument then by default server will start with 8282. If we want a different port, then we need to pass desired port in argument as follows. Suppose we have an executable JAR named as my-app.jar.

Using --port

java -jar my-app.jar --port=8585 

Using -Dport

java -jar -Dport=8585 my-app.jar 

Server will start with 8585 port.

Using SERVER_PORT with SpringApplication Programmatically

SpringApplication has a method as setDefaultProperties() that is used to change spring boot default properties. Suppose we want to change default port then we need to create a Map and put a port with SERVER_PORT key. Find the example.

package com.humoyun;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class MyApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        Map<String, Object> map = new HashMap<>();
        map.put("SERVER_PORT", "8585");

Spring boot will start the server with 8585 port.

answered 11 months ago Moe Pad #33

This question is the first result if you google for Gradle Spring Port.

If you use gradle you can do something like this if you have the Spring Boot Gradle Plugin already applied:

bootRun {
    args += ["--server.port=[PORT]"]

For a more sophisticated answer please see my answer here.

answered 11 months ago saravanan #34

If you are working over boot projects and you wanna configure the port you can give the input in the file like NOTE:properties file should be under src/main/resource

Spring properties

server.port=9999 If you using the CMD then follow this command -Dserver.port=9999 For default port its server.port=0 Make sure no port is using this port number

answered 9 months ago deepesh kumar #35

There are many other stuffs you can alter in server configuration by changing Like session time out, address and port etc. Refer below post


I used few of them as below.

server.port = 3029
server.address= deepesh

answered 5 months ago Sagar Mal Shankhala #36

Hope this one help> 




answered 3 days ago Ashwini #37

Providing the port number in file will resolve the issue

 server.port = 8080

"port depends on your choice, where you want to host the application"

answered 2 days ago Naga Praneeth #38

There are three ways to do it depending on the application configuration file you are using

  1. If you are using file set

server.port = 8090 2. If you are using application.yml file set server port property in YAML format as given below

server: port: 8090 3. You can also Set the property as the System property in the main method


comments powered by Disqus