Tuesday, April 21, 2020

How to set/configure port for a Spring Boot application


When we start developing Spring Boot applications, we go on creating more than one applications and try to start them on our local environment.

This is very simple problem but Spring Boot beginners might face it as by default all the applications created through popular IDEs(Spring Tool Suite, intelliJ) have port set to 8080.

Using any of the following ways we can set application to run on any particular port.

According to your convenience you can choose to work with *.properties or *.yml file which resides in /src/main/resources.

If you are using application.yml or application.yaml you can set the port using:

server:
  port: 9090

If you are using application.properties you can set the port using:

server.port = 9090

If you want to use random port every time you start the application you can use:

server:
  port: 0

Or

server.port = 0

Also you might want to make changes to Environment variables temporarily as you may be debugging the application or something similar, then you can add following as your Environment variable or VM argument:

-Dserver.port=9090
Or you can set Environment variable as: 
SERVER_PORT=9090

Any one of the above methods can help you change the port of where application is running, according to me changing *.properties or *.yml will be simplest but you can use any of the above solutions to get going easily.

Hope this post helps you !

Sunday, January 5, 2020

Spring Boot - APPLICATION FAILED TO START: Web server failed to start. Port 8080 was already in use.


This is most commonly occurring error that troubles Spring Boot Beginners.

Sometimes even after stopping the Spring Boot application from IDE, process running on a particular port (in this case 8080) is not killed and following error occurs. 

Also visit another blog by me: www.productfactsheet.com


01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m      [2m (v2.2.2.RELEASE) [0;39m
[2m2020-01-05 14:31:13.996 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mc.sud.demo.SudarshanStarterApplication  [0;39m [2m: [0;39m Starting SudarshanStarterApplication on FWS-CHE-LT-3266.local with PID 23513 (/Users/sdhiray/Documents/workspace-sts-3.9.11.RELEASE/SudarshanStarter/target/classes started by sdhiray in /Users/sdhiray/Documents/workspace-sts-3.9.11.RELEASE/SudarshanStarter)
[2m2020-01-05 14:31:13.999 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mc.sud.demo.SudarshanStarterApplication  [0;39m [2m: [0;39m No active profile set, falling back to default profiles: default
[2m2020-01-05 14:31:14.556 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate [0;39m [2m: [0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2020-01-05 14:31:14.613 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate [0;39m [2m: [0;39m Finished Spring Data repository scanning in 44ms. Found 1 JPA repository interfaces.
[2m2020-01-05 14:31:14.835 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mtrationDelegate$BeanPostProcessorChecker [0;39m [2m: [0;39m Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2m2020-01-05 14:31:15.076 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m: [0;39m Tomcat initialized with port(s): 8080 (http)
[2m2020-01-05 14:31:15.084 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m: [0;39m Starting service [Tomcat]
[2m2020-01-05 14:31:15.084 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m: [0;39m Starting Servlet engine: [Apache Tomcat/9.0.29]
[2m2020-01-05 14:31:15.136 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m: [0;39m Initializing Spring embedded WebApplicationContext
[2m2020-01-05 14:31:15.136 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.s.web.context.ContextLoader           [0;39m [2m: [0;39m Root WebApplicationContext: initialization completed in 1099 ms
[2m2020-01-05 14:31:15.253 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m: [0;39m HikariPool-1 - Starting...
[2m2020-01-05 14:31:15.589 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m: [0;39m HikariPool-1 - Start completed.
[2m2020-01-05 14:31:15.679 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m: [0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2020-01-05 14:31:15.754 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36morg.hibernate.Version                   [0;39m [2m: [0;39m HHH000412: Hibernate Core {5.4.9.Final}
[2m2020-01-05 14:31:15.885 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.hibernate.annotations.common.Version  [0;39m [2m: [0;39m HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
[2m2020-01-05 14:31:15.976 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m: [0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
[2m2020-01-05 14:31:16.433 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m: [0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[2m2020-01-05 14:31:16.439 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mj.LocalContainerEntityManagerFactoryBean [0;39m [2m: [0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2020-01-05 14:31:16.662 [0;39m [33m WARN [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mJpaBaseConfiguration$JpaWebConfiguration [0;39m [2m: [0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[2m2020-01-05 14:31:16.775 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.s.s.concurrent.ThreadPoolTaskExecutor [0;39m [2m: [0;39m Initializing ExecutorService 'applicationTaskExecutor'
[2m2020-01-05 14:31:16.987 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m: [0;39m Stopping service [Tomcat]
[2m2020-01-05 14:31:17.005 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mConditionEvaluationReportLoggingListener [0;39m [2m: [0;39m
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2020-01-05 14:31:17.009 [0;39m [31mERROR [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter  [0;39m [2m: [0;39m
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 8080 was already in use.
Action:
Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.
[2m2020-01-05 14:31:17.011 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mo.s.s.concurrent.ThreadPoolTaskExecutor [0;39m [2m: [0;39m Shutting down ExecutorService 'applicationTaskExecutor'
[2m2020-01-05 14:31:17.011 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mj.LocalContainerEntityManagerFactoryBean [0;39m [2m: [0;39m Closing JPA EntityManagerFactory for persistence unit 'default'
[2m2020-01-05 14:31:17.012 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m: [0;39m HikariPool-1 - Shutdown initiated...
[2m2020-01-05 14:31:17.015 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[           main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m: [0;39m HikariPool-1 - Shutdown completed.
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 [32m :: Spring Boot ::  [39m       [2m (v2.2.2.RELEASE) [0;39m

 [2m2020-01-05 14:31:13.996 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mc.sud.demo.SudarshanStarterApplication   [0;39m  [2m: [0;39m Starting SudarshanStarterApplication on FWS-CHE-LT-3266.local with PID 23513 (/Users/sdhiray/Documents/workspace-sts-3.9.11.RELEASE/SudarshanStarter/target/classes started by sdhiray in /Users/sdhiray/Documents/workspace-sts-3.9.11.RELEASE/SudarshanStarter)
 [2m2020-01-05 14:31:13.999 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mc.sud.demo.SudarshanStarterApplication   [0;39m  [2m: [0;39m No active profile set, falling back to default profiles: default
 [2m2020-01-05 14:31:14.556 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36m.s.d.r.c.RepositoryConfigurationDelegate [0;39m  [2m: [0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
 [2m2020-01-05 14:31:14.613 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36m.s.d.r.c.RepositoryConfigurationDelegate [0;39m  [2m: [0;39m Finished Spring Data repository scanning in 44ms. Found 1 JPA repository interfaces.
 [2m2020-01-05 14:31:14.835 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mtrationDelegate$BeanPostProcessorChecker [0;39m  [2m: [0;39m Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 [2m2020-01-05 14:31:15.076 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.s.b.w.embedded.tomcat.TomcatWebServer  [0;39m  [2m: [0;39m Tomcat initialized with port(s): 8080 (http)
 [2m2020-01-05 14:31:15.084 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.apache.catalina.core.StandardService   [0;39m  [2m: [0;39m Starting service [Tomcat]
 [2m2020-01-05 14:31:15.084 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36morg.apache.catalina.core.StandardEngine  [0;39m  [2m: [0;39m Starting Servlet engine: [Apache Tomcat/9.0.29]
 [2m2020-01-05 14:31:15.136 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.a.c.c.C.[Tomcat].[localhost].[/]       [0;39m  [2m: [0;39m Initializing Spring embedded WebApplicationContext
 [2m2020-01-05 14:31:15.136 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.s.web.context.ContextLoader            [0;39m  [2m: [0;39m Root WebApplicationContext: initialization completed in 1099 ms
 [2m2020-01-05 14:31:15.253 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mcom.zaxxer.hikari.HikariDataSource       [0;39m  [2m: [0;39m HikariPool-1 - Starting...
 [2m2020-01-05 14:31:15.589 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mcom.zaxxer.hikari.HikariDataSource       [0;39m  [2m: [0;39m HikariPool-1 - Start completed.
 [2m2020-01-05 14:31:15.679 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.hibernate.jpa.internal.util.LogHelper  [0;39m  [2m: [0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
 [2m2020-01-05 14:31:15.754 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36morg.hibernate.Version                    [0;39m  [2m: [0;39m HHH000412: Hibernate Core {5.4.9.Final}
 [2m2020-01-05 14:31:15.885 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.hibernate.annotations.common.Version   [0;39m  [2m: [0;39m HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
 [2m2020-01-05 14:31:15.976 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36morg.hibernate.dialect.Dialect            [0;39m  [2m: [0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
 [2m2020-01-05 14:31:16.433 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.h.e.t.j.p.i.JtaPlatformInitiator       [0;39m  [2m: [0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
 [2m2020-01-05 14:31:16.439 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mj.LocalContainerEntityManagerFactoryBean [0;39m  [2m: [0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
 [2m2020-01-05 14:31:16.662 [0;39m  [33m WARN [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mJpaBaseConfiguration$JpaWebConfiguration [0;39m  [2m: [0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
 [2m2020-01-05 14:31:16.775 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.s.s.concurrent.ThreadPoolTaskExecutor  [0;39m  [2m: [0;39m Initializing ExecutorService 'applicationTaskExecutor'
 [2m2020-01-05 14:31:16.987 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mo.apache.catalina.core.StandardService   [0;39m  [2m: [0;39m Stopping service [Tomcat]
 [2m2020-01-05 14:31:17.005 [0;39m  [32m INFO [0;39m  [35m23513 [0;39m  [2m--- [0;39m  [2m[           main] [0;39m  [36mConditionEvaluationReportLoggingListener [0;39m  [2m: [0;39m 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

[2m2020-01-05 14:31:17.009 [0;39m [31mERROR [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[ main] [0;39m [36mo.s.b.d.LoggingFailureAnalysisReporter [0;39m [2m: [0;39m ***************************

APPLICATION FAILED TO START

*************************** Description:

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

Action:

Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.

[2m2020-01-05 14:31:17.011 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[ main] [0;39m [36mo.s.s.concurrent.ThreadPoolTaskExecutor [0;39m [2m: [0;39m Shutting down ExecutorService 'applicationTaskExecutor' [2m2020-01-05 14:31:17.011 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[ main] [0;39m [36mj.LocalContainerEntityManagerFactoryBean [0;39m [2m: [0;39m Closing JPA EntityManagerFactory for persistence unit 'default' [2m2020-01-05 14:31:17.012 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[ main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m: [0;39m HikariPool-1 - Shutdown initiated... [2m2020-01-05 14:31:17.015 [0;39m [32m INFO [0;39m [35m23513 [0;39m [2m--- [0;39m [2m[ main] [0;39m [36mcom.zaxxer.hikari.HikariDataSource [0;39m [2m: [0;39m HikariPool-1 - Shutdown completed.




Solution:

It can be resolved by following given steps(in accordance with your OS):

WINDOWS

1. Open command prompt & Check what processes are running at available ports

netstat -ao |find /i "listening"

We get following kind of output:

TCP    0.0.0.0:7981           machinename:0        LISTENING       2428
TCP    0.0.0.0:7982           machinename:0        LISTENING       2428
TCP    0.0.0.0:8080           machinename:0        LISTENING       12704
TCP    0.0.0.0:8500           machinename:0        LISTENING       2428

i.e. Port Numbers and what Process Id they are listening to

2. Stop process running at your port number. (In this case it is 8080 & Process Id is 12704)

Taskkill /F /IM 12704

Please mention correct PID, otherwise you might end up killing some other important process.

Other solution could be:
1. Go to Task Manager and kill java(Java(TM) Platform SE binary) process (It will close your IDE, which might not be desired)
2. Restart your IDE

MacOS 

1. Open Terminal & Check what process is running at port in error

sudo lsof -i tcp:8080 (Mention port number in error)

OR

netstat -vanp tcp | grep 3000

We get following kind of output(using lsof):

COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
name    22758 user   16u  IPv4 0x449ce55d62d46bc7      0t0  TCP localhost:hbci (LISTEN)
name    22758 user   17u  IPv6 0x449ce55d5f4a1a87      0t0  TCP localhost:hbci (LISTEN)


i.e. Process Ids that are running on port.

2. Stop process running at your port number. (In this case it is 8080 & Process Id is 12704)

kill -9 22758

Please mention correct PID, otherwise you might end up killing some other important process.

LINUX 

It is same as that of MacOS

1. Open Terminal & Check what process is running at port in error

sudo lsof -i tcp:8080 (Mention port number in error)

OR

netstat -vanp tcp | grep 3000

We get following kind of output(using lsof):

COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    23672 sine  238u  IPv6 3028222      0t0  TCP localhost:http-alt (LISTEN)


i.e. Process Ids that are running on port.

2. Stop process running at your port number. (In this case it is 8080 & Process Id is 12704)

kill -9 22758

Please mention correct PID, otherwise you might end up killing some other important process.

Hope this post helps you !
In case you have any better solutions please add your comments 😊