In my previous article Spring Boot Introduction Tutorial, we have learned all the basics of Spring Boot such as Entry class, @SpringBootApplication, @ComponentScan, etc.
In this article we will look how to run application from Spring Boot CLI.
Overview of things you will learn in this tutorial
- What is Spring Boot CLI?
- Installation of Spring Boot CLI
- “spring” command.
- Run the Spring Boot HelloWorld.
What is Spring Boot CLI?
It is a command-line application or tool which is used to create, run, and test the spring boot application from the command line itself. Spring Boot CLI contains the Groovy script which configures all the dependencies automatically when you run the Spring Boot application from the CLI tool.
Groovy script: It is a programming as well as the scripting language, the syntax is almost the same as java. It is developed to use as both scripting and programming language together on the Java platform.
Steps to download and install Spring Boot CLI
You can download and install by using any of the following method.
- Install Manually.
- Using SDKMAN Installation.
- Installation from OSX Homebrew.
- Using MacPorts.
- Window Scoop Installation.
I will explain with the first approach to download and install, which is Manual Installation. You can refer to the official documentation for further help from here https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-spring-boot
Note: Please refer to the above link for download. For manual download and install you have two options that are
- spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.zip
- spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.tar.gz
You can choose either of them but the first one that is .zip is very easy to download. Once download completed unzip the folder. You will get a folder with the name spring-2.3.2.RELEASE go inside that there will be a bin folder.
Copy the path and set the environment variable as you do for jdk. To make sure Spring CLI is properly installed on your computer. Go to the command prompt and enter the following command spring –-version. If everything is correct then you will see the screen as below.
Run HelloWorld Example with Spring Boot CLI
In this section, you will learn how to write and run the groovy script. Please follow all the steps mentioned below.
Steps 1: Create one folder with the name HelloWorld anywhere on your local computer. I am creating a folder on my the desktop screen, so the path is C:\Users\1302143\Desktop
Step 2: Open a notepad or any text file under that folder and write following code.
@Controller
class HelloWorld {
@RequestMapping("/")
@ResponseBody
public String hello() {
"Welcome to Spring Boot CLI"
}
}
And save it as HelloWorld.groovy inside the HelloWorld Folder. So the path for this file is C:\Users\1302143\Desktop\HelloWorld\HelloWorld.groovy
This file is only called a groovy script. So it is very important that you should always save the file with an extension of .groovy.
Note: If you are using notepad for writing the above code then by default it will save as HelloWorld.groovy.txt but you need to change the extension HelloWorld.groovy
Run the HelloWorld.groovy
Step 1: Once you save the file inside HelloWorld Folder, now opens the command prompt inside the same folder.
Note: Inside the command prompt the path should will C:\Users\1302143\Desktop\HelloWorld
Step 2: Write a command spring run HelloWorld.groovy
Step 3: Press enter. Boom! Now you see inside the command prompt. the spring boot has started running.
Step 4: Go to localhost:8080 in your browser. Now you see the output screen will be like as shown below.
Step 5: Simply close the command prompt terminal to terminate or stop the spring boot application.
Note: For running the application through Spring Boot CLI, no IDE such as Eclipse or IntelliJ has required.
Summary
In this tutorial, we have learned to Run Application from Spring Boot CLI. In the next article, I covered how to start working with Spring Boot in IDE.
If you have queries or suggestions regarding my article please do comment down, I will love to resolve the issue or doubt if you are facing. Please give me feedback about my article. You can suggest me anytime how can I provide more valuable content to you.