Anonymous Creator
Anonymous Creator

Reputation: 3789

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

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.

Upvotes: 74

Views: 458682

Answers (27)

MonirRouissi
MonirRouissi

Reputation: 631

You can use npm to kill the port

npx kill-port 8080   //8080 for example

Requirement: npm

read more: https://www.npmjs.com/package/kill-port

Upvotes: 18

In mac or linux.

Step 1: Run lsof -i :8080 in terminal

Step 2: Catch PI

Step 3: Run kill -9 PID

Upvotes: 2

Kinyua
Kinyua

Reputation: 147

if on windows, open CMD and type taskkill /im javaw.exe /f

Upvotes: 2

sbrajchuk
sbrajchuk

Reputation: 183

I repeatedly encountered the same issue on my Windows 10 system, wherein various diagnostic tools consistently reported no other processes utilizing the same port. Strangely, the problem persisted, but a simple reboot consistently resolved it. To avoid restarting and having to relaunch numerous applications, I found an alternative solution: I restarted the Host Network Service (hns) with the following commands:

net stop hns
net start hns

My research led me to a Stack Overflow post (https://stackoverflow.com/a/62359555/19541281) explaining that Windows reserved specific port ranges as the underlying cause.

In my particular case, Windows had reserved the following port range:

netsh int ipv4 show dynamicport tcp

Protocol tcp Dynamic Port Range
--------------------------------
Start Port : 1024
Number of Ports : 13977

I altered the Windows port reservation range by executing this command:

netsh int ipv4 set dynamic tcp start=49152 num=16384

Subsequently, I performed a host reboot to ensure the changes took effect.

Upvotes: 5

Manish
Manish

Reputation: 131

Restarting my machine solved the issue although I was getting below issue:

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 8081 is already in use

Upvotes: -1

Murat Çelik
Murat Çelik

Reputation: 31

To reset port 8080, you need to find PID (Process ID) and specify it for the command as, for example, on the screen 10512:

taskkill /F /PID 10512

enter image description here

Upvotes: 3

Vishal Bramhankar
Vishal Bramhankar

Reputation: 263

open command prompt as administrator

step1: netstat -ano | findstr :<enter your 4 digit port number>

netstat -ano | findstr :8080

TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 6436

TCP [::]:8080 [::]:0 LISTENING 6436

step2: taskkill /PID <enter above pid number(sometimes it shown 3/4/5/6 digits)> /F

taskkill /PID 6436 /F

SUCCESS: The process with PID 6436 has been terminated.

Upvotes: 7

TMB
TMB

Reputation: 41

  1. Open cmd and type "netstat -ano -p tcp."
  2. Then, look for the port number and PID.
  3. Open up Resource Monitor and search for the PID number
  4. Right Click and "End Process."

Upvotes: 0

Kavita
Kavita

Reputation: 11

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

Upvotes: -1

Lakshya Sharma
Lakshya Sharma

Reputation: 31

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

Upvotes: 3

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

Upvotes: 0

crwils
crwils

Reputation: 868

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

Upvotes: 52

Afreen Shaikh
Afreen Shaikh

Reputation: 67

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.

Upvotes: 6

Leena
Leena

Reputation: 783

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.

Upvotes: 3

Oleksii Kyslytsyn
Oleksii Kyslytsyn

Reputation: 2426

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.

}

Upvotes: 1

Supun Sandaruwan
Supun Sandaruwan

Reputation: 2418

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

Upvotes: 34

Gustavo Passini
Gustavo Passini

Reputation: 2668

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).

Upvotes: 41

Feezan Khattak
Feezan Khattak

Reputation: 222

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 :)

Upvotes: 4

User
User

Reputation: 1628

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

Upvotes: 4

Cihan Icelliler
Cihan Icelliler

Reputation: 33

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

Upvotes: 0

kmantic
kmantic

Reputation: 5

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.sstatic.net/ICGtX.png

Upvotes: -2

Priti Yogi
Priti Yogi

Reputation: 1

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.

Upvotes: 0

Rashmi Kiran
Rashmi Kiran

Reputation: 91

  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)

Upvotes: 9

Alan Carlyle
Alan Carlyle

Reputation: 1143

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.

Upvotes: 90

jayesh
jayesh

Reputation: 921

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

server.port = 8090

Upvotes: 62

Armen Arzumanyan
Armen Arzumanyan

Reputation: 2043

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.

Upvotes: 0

Dominik
Dominik

Reputation: 353

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

Upvotes: 1

Related Questions