Overview
In the Spring Boot opening article - Create and Run, it describes how to create a Sprint Boot project and run it. But the way to run is run directly in IDEA. There is another way to run the Spring Boot program, which is to execute the jar package directly on the command line.
Make it into a jar package
Previous WEB programs needed to be packaged into WAR packages and deployed to Tomcat. Spring Boot supports packaged into JAR, even if the JAR contains pictures, pages, etc., it is supported. In addition, using JAR packages is also convenient to deploy to Docker.
To package Spring Boot into JAR, you need to add the following code to the POM.xml file:
<groupId>com.springboot</groupId><artifactId>study</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging>
Change package to jar. A Maven plugin is also needed.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
You can use the following operations in IDEA to form a JAR package.
Click on the Maven Projects on the right side of IDEA, click on the red part in the pop-up window, and execute the Maven command.
After clicking, a window to execute the Maven command will appear. Enter
clean package
You can perform the packaging operation.
After successful execution, the corresponding JAR package can be found in the target directory in the project directory.
study-0.0.1-SNAPSHOT.jar
Execute JAR packages
Start a CMD or other command line tool, execute the following command, and the job is done.
java -jar study-0.0.1-SNAPSHOT.jar
You can directly enter it in the browser
http://localhost:8080/hello
The browser will output
hello, Spring Boot
For specific code, please refer to the previous Spring Boot opening - Creation and Run.
Summarize
The above is the method of directly running projects in Spring Boot introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!