How to Run Spring Boot Application using Command Line?

How to Run Spring Boot Application using Command-Line? Though there are multiple ways in which we can Run the Spring boot Application, here, in this article, we will see How to Run Spring Boot Application using Command-Line?

Following are the two ways to run the Spring Boot Application through the Command-Line.

  • Run it using mvn applicationName: run maven plugin.
  • Run it using the java-jar command.

we will now see How to run an application using the java – jar command. We will create a simple demo example to show How to Run Spring Boot Application using Command-Line?

Create Project

Create a project using Spring Initializr. If you don’t know how to create a project using the same check this article https://codedec.com/tutorials/how-to-create-spring-boot-project-using-spring-initializr/

Import a Project

Now, it’s time to import the project into STS. Go to File > Import > Maven > Existing Maven Project > Next > Browse > Select the project > Finish. Now it will take time to import the project and will import all the dependencies in case you added.

Configure application. properties file

#change the port
server.port = 8888

Create a Controller

Create a TestController.java inside com.example.demo.ctl package and annotate it with @Controller. The request for the web page will be handle by the handler methods in the controller class using @GetMapping

package com.example.demo.ctl;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

 @ResponseBody
 @GetMapping("/")
 public String home() {
  return "Run Application using CMD Line!!";
 }
}

Now, Right-click on the project name > Maven Install. After successful installation of it, there will be jar file will be created inside the target directory of your project as shown below.

Now, Go to the Command prompt and type the following commands.

Now here you see the Application is started. Go to localhost:8888 and see the following output

In this way, we have learned How to Run Spring Boot Application using Command-Line.