52

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.

Anonymous Creator
  • 2,968
  • 7
  • 31
  • 77
  • 2
    You are asking the same question. Your port 8080 is already bound, you can not run Spring boot in the same port. Go to command prompt in windows and type the command netstat -a, you will get to know which is running in port 8080. – Sambit Jun 07 '19 at 19:03
  • sorry I am very new to spring boot. But why does it require port. I need it to run as microservice. if I dont add this dependency it is working fine. – Anonymous Creator Jun 07 '19 at 19:05
  • Any web server has to use any port to run. This is fundamental concept. First try to spring boot with 1.5.0, take some example from internet. – Sambit Jun 07 '19 at 19:07
  • @AnonymousCreator if there was no port how would you access your server? – mavriksc Jun 07 '19 at 19:10
  • can't I just use their webclient method? why it is a web server. it is just a console application. which I am using as a microservice. spring boot is not for web application only as what I can see from internet. – Anonymous Creator Jun 07 '19 at 19:11
  • 1
    Even though this question is about using the web client without using the application as a web app, most were led here because of google. For them, you are trying to running two application over the same port. So kill the already running application or run the new application on a different port. You can find more details at https://springhow.com/web-server-failed-to-start-port-8080-was-already-in-use/. – Raja Anbazhagan May 27 '21 at 15:15

25 Answers25

77

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.

Alan Carlyle
  • 948
  • 1
  • 6
  • 11
52

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

server.port = 8090

jayesh
  • 761
  • 6
  • 4
  • This worked for me!!! I had similar issue and tired all possible way mentioned in the article but none worked. Then found the real problem that was causing this issue. I had JDK 17 and JDK 8 versions installed in my machine. So when i started spring boot application, it resulted in Web server failed to start. Port 8080 was already in use. Spring Boot. I configured JDK 17 to my IDE but when i start my server, both Java JDK gets starting and creating issue. So to solve this issue, you need to uninstall any of the JDK version and do a try again. – Sakthivel Subbaiyan Nov 27 '22 at 09:17
38

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

Gustavo Passini
  • 2,348
  • 19
  • 25
  • thts wht I wanted. so this only start when webflux is there? – Anonymous Creator Jun 07 '19 at 20:41
  • 1
    Not only webflux can trigger the embedded server to start automatically, as the doc mentions: **If your classpath contains the necessary bits to start a web server, Spring Boot will automatically start it**. The needed classes might be added by other Spring modules. – Gustavo Passini Jun 10 '19 at 19:15
35

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
crwils
  • 631
  • 1
  • 7
  • 11
29

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

Supun Sandaruwan
  • 1,814
  • 19
  • 19
  • 1
    The most accurate answer I was looking for, thanks man ! This Answer should have been on the top ! – Divya J Aug 24 '21 at 13:28
16

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

MonirRouissi
  • 549
  • 8
  • 7
  • Install NPM pacakage manager and try this command, it works perfectly fine.but when made changes on the code sometimes doesn't refresh the server automatically u have to kill the process and re-run project. – Suresh B B Jan 22 '22 at 19:07
9
  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)
Rashmi Kiran
  • 91
  • 1
  • 1
6

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.
Afreen Shaikh
  • 67
  • 1
  • 1
4

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

User
  • 1,460
  • 14
  • 11
4

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

Feezan Khattak
  • 212
  • 3
  • 15
3

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.

Leena
  • 703
  • 1
  • 12
  • 21
3

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.

2

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

invzbl3
  • 5,872
  • 9
  • 36
  • 76
1

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
Dominik
  • 353
  • 1
  • 5
1

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.

}
Oleksii Kyslytsyn
  • 2,458
  • 2
  • 27
  • 43
1

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
Dada
  • 6,313
  • 7
  • 24
  • 43
0

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.

Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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

0

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

0
  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."
TMB
  • 31
  • 2
0

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
Manish
  • 133
  • 2
  • 8
0

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.

sbrajchuk
  • 48
  • 6
-1

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

Kavita
  • 1
  • 1
    this is incorrect, server.port can go anywhere in the application.properties file, and if something else is already using the port, it won't help at all. – PaulNUK Mar 13 '22 at 17:58
-2

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

kmantic
  • 7
  • 1