欢迎访问我的GitHub
https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
在开发springboot应用时,通过java -jar命令启动应用是常用的方式,今天就来一起了解这个简单操作背后的技术;
开发demo开发一个springboot应用作为本次研究的对象,对应的版本信息如下:
JDK:1.8.0_211
springboot:2.3.1.RELEASE
maven:3.6.0
接下来开发springboot应用,这个应用异常简单:
springboot应用名为springbootstarterdemo,pom.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bolingcavalry</groupId> <artifactId>springbootstarterdemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootstarterdemo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>只有一个java类,里面有个http接口:
package com.bolingcavalry.springbootstarterdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @SpringBootApplication @RestController public class SpringbootstarterdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootstarterdemoApplication.class, args); } @RequestMapping(value = "/hello") public String hello(){ return "hello " + new Date(); } }编码完成,在pom.xml所在目录执行命令
mvn clean package -U -DskipTests构建成功后,在target目录下得到文件springbootstarterdemo-0.0.1-SNAPSHOT.jar
就是这个springbootstarterdemo-0.0.1-SNAPSHOT.jar,此时执行java -jar springbootstarterdemo-0.0.1-SNAPSHOT.jar就能启动应用,如下图:
接下来就用这个springbootstarterdemo-0.0.1-SNAPSHOT.jar来分析jar文件能够独立启动的原因;
java -jar做了什么
先要弄清楚java -jar命令做了什么,在oracle官网找到了该命令的描述:
If the -jar option is specified, its argument is the name of the JAR file containing class and resource files for the application. The startup class must be indicated by the Main-Class manifest header in its source code.
再次秀出我蹩脚的英文翻译:
使用-jar参数时,后面的参数是的jar文件名(本例中是springbootstarterdemo-0.0.1-SNAPSHOT.jar);
该jar文件中包含的是class和资源文件;
在manifest文件中有Main-Class的定义;
Main-Class的源码中指定了整个应用的启动类;(in its source code)
小结一下:
java -jar会去找jar中的manifest文件,在那里面找到真正的启动类;
springbootstarterdemo-0.0.1-SNAPSHOT.jar是前面的springboot工程的构建结果,是个压缩包,用常见的压缩工具就能解压,我这里的环境是MacBook Pro,用unzip即可解压;
解压后有很多内容,我们先关注manifest相关的,下图红框中就是manifest文件:
打开上图红框中的文件,内容如下:
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx Implementation-Title: springbootstarterdemo Implementation-Version: 0.0.1-SNAPSHOT Start-Class: com.bolingcavalry.springbootstarterdemo.Springbootstarter demoApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Build-Jdk-Spec: 1.8 Spring-Boot-Version: 2.3.1.RELEASE Created-By: Maven Jar Plugin 3.2.0 Implementation-Vendor: Pivotal Software, Inc. Main-Class: org.springframework.boot.loader.JarLauncher