Web server failed to start. Port 8080 was already in use. Spring Boot microservice

Viewed 236909

I am trying to call webAPI from gradle project.

My build.gradle is as following.

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    runtimeOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-webflux'
    compile 'org.projectreactor:reactor-spring:1.0.1.RELEASE'
}

If I remove following dependency

compile 'org.springframework.boot:spring-boot-starter-webflux'

It works, but if I add it back. it gives error as

Web server failed to start. Port 8080 was already in use.

So, how do I fix this, so that I can use webclient? Because application is not web application which requires port to run. it is a sort of microservice.

I just want to use WebClient of Spring Boot. How do i use it without converting my application into web application.

21 Answers

If on windows and your getting this every time you run the application you need to keep doing:

> netstat -ano | findstr *<port used>*

  TCP    0.0.0.0:*<port used>*  0.0.0.0:0              LISTENING       *<pid>*
  TCP    [::]:*<port used>*     [::]:0                 LISTENING       *<pid>*

> taskkill /F /PID *<pid>*
SUCCESS: The process with PID *<pid>* has been terminated.

If netstat above includes something like this;

TCP    [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:540yy [zzzz:e2ce:44xx:1:axx6:dxxf:xxx:xxxx]:*<port used>* TIME_WAIT 0

Then you can either wait for a little while or reconfigure to use another port.

I suppose we could write some code to randomly generate and check if a port is free when the application runs. Though this will have diminishing returns as they start to get used up. On the other hand could add a resource clean up code that does what we have above once the application stops.

You can change the default port of your application in application.properties by adding the following line:

server.port = 8090

If you don't want the embedded server to start, just set the following property in you application.properties (or .yml):

spring.main.web-application-type=none

If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it. To disable this behaviour configure the WebApplicationType in your application.properties

Source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html


If you application really is a Web application, then you can easily change the port using the server.port property (in your application's .properties/.yaml file, as a command line argument at startup, etc).

Some time if you can manually kill the that port problem can solve.

Using CurrPorts software you can view what are the running all ports in your machine and you can kill that port if you want.

You can download CurrPorts from here. (download link is in bottom of the page)

enter image description here

============== OR ==============

Without CurrPorts you can do this using like below method also.

  1. CMD as run as administrator
  2. Enter netstat -a -o -n and hit enter. Now you can see like below. Port can see Local Address column after : sign.

enter image description here

  1. select the process id(not port) that your port running and type taskkill /F /PID <process_id_here> command and hit enter.

enter image description here

Another way to do this is by first checking what processes are using that specific port, then killing it using its process ID:

  1. Run lsof -i :8080 This will identify which process is listening on port 8080.

  2. Take note of the process ID (PID) e.g. 63262

  3. Run kill -9 <PID> e.g. kill -9 63262

  1. create /resources folder inside src/main
  2. create application.properties file inside /resources
  3. write server.port=9090 //(use any port number of your choice)

if port:8080 already in use error occurs:

  • goto command prompt .type command> .netstat -ano .Enter->this will show all running port check port 8080 .type command> taskkill /F /PID 8080 .process will terminate.

you can set server.port= #some-available-port number in application.properties file

or run command prompt in administrator mode and run netstat -a -o -n. Find the process id which is using port 8080.

Run taskkill /F /PID #Processid command

It's easy we have two methods to solve. First one is to change the port number in your application.properties i.e

server.port=9999 //  something like this...

and second is, to first stop the available running server and then re-run your server again.

I am sure it work :)

If you had previously started the spring boot app and forgot to stop before hitting play again then Go to windows task manager and locate the java application(Something like "OpenJDK Platform binary" and click on End Task. (Java app not eclipse). Then try running again. It worked for me.

You try to use an already used port.

Ports are used on the transport layer - tcp, http is application layer and uses a transport layer to send and receive requests.

Default port exposed by spring boot app is 8080. In your case you have two solutions:

  • change port for your application
  • stop the service that uses the port you want to use

to catch java.net.BindException e with message: Address already in use and to start on other available port and to use webclient with one of 2 ports.

try {
            SpringApplication.run(Application.class, args);
        } catch (org.springframework.boot.web.server.PortInUseException e) {
//Runtime.exec("pkil")..
//or
SpringApplication.run(Application.class, otherargs);
//SpringApplication.run(Application.class, new String[]{"--server.port=8444"});
//when invoked recursively it is a port rebalancer for port usage among port pool with server as from client for startup stage via application restarts within many busy ports which are used before or without querying.

}

The error basically means that your port 8080 is occupied. If you are getting this error then go to your project and open application.properties and add the below line and it should work fine:

server.port = 8090

Your client application also spring boot application, whats why you have two spring boot application run in 8080 port. Change port one of them or create a standalone java application with main class, put your web client in it and run. As http client you can use Apache Http Client.

Today I was working in Spring Boot project and I got the same error in my project. So, what I did, I clicked on stop button beside the run last tool button and run again. Then, my project started working very well.

You can also write services.msc in your search bar in windows.Then you can find Apache Tomcat and then just stop this apache. I think its gonna be work.Find Apache TomCat and Stop it

It is a simple answer.If you are getting this error then go to your project then

src/main/resources and open application.properties file and mention there

server.port=8045 you can give your own number here instead of 8045

thanksenter image description here

If you getting this error again and again , then make sure server.port=(your port no) is the first line in application.properties file.

The best answer to this is to always use the "Relaunch Application" button. That will stop the web server and restart the entire app on the same port as before.

It's the button with the red square and green play icon combined.

https://i.stack.imgur.com/ICGtX.png

Related