Configure server port when running jar

Caution Always pass the -D<key>=<value> JVM parameters before the -jar arguments otherwise it wouldn't accept your parameters and then it will run with default values. e.g:

Correct java command to execute the jar on a particular port is:

java -Dserver.port=8888 -jar target/my-application-jar-path.jar

The above command will run the JVM on the port 8888 but the below command

java -jar target/my-application-jar-path.jar -Dserver.port=8888

Will run on the port 8080, it will ignore the JVM parameters after -jar

Best practice in spring-boot application is to set the server.port into the application.properties file as:

server.port=9090

Or on the particular application-<ENV>.properties file with specific ENVIROMENT.


From Spring boot documentation, the command line is :

java -Dserver.port=8888 -jar myApplication.jar

You can also use Spring boot configuration file as described in the documentation.