编程知识 cdmana.com

Why can springboot jar run independently

Welcome to visit mine GitHub

https://github.com/zq2599/blog_demos

Content : All original articles classified summary and supporting source code , involve Java、Docker、Kubernetes、DevOPS etc. ;

Welcome to visit mine GitHub

https://github.com/zq2599/blog_demos
Content : All original articles classified summary and supporting source code , involve Java、Docker、Kubernetes、DevOPS etc. ;

Capable of operating independently jar file

Developing springboot When applied , adopt java -jar Command to start an application is a common way , Today, let's learn about the technology behind this simple operation ;

Development demo

To develop a springboot Application as the object of this study , The corresponding version information is as follows :

  • JDK:1.8.0_211
  • springboot:2.3.1.RELEASE
  • maven:3.6.0

Next, develop springboot application , This application is very simple :

  1. springboot Application name is springbootstarterdemo,pom.
<?
  1. only one java class , There's a http Interface :
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@RestControllerpublic class SpringbootstarterdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootstarterdemoApplication.class, args); } @RequestMapping(value = "/hello") public String hello(){ return "hello " + new Date(); }}
  1. Coding complete , stay pom.
mvn clean package -U -DskipTests
  1. After a successful build , stay target Directory to get the file springbootstarterdemo-0.0.1-SNAPSHOT.jar
  2. This is this. springbootstarterdemo-0.0.1-SNAPSHOT.jar, Execute at this time java -jar springbootstarterdemo-0.0.1-SNAPSHOT.jar You can start the app , Here's the picture :
     Insert picture description here
    Next use this springbootstarterdemo-0.0.1-SNAPSHOT.jar To analyze jar Why files can be started independently ;

java -jar What did you do

  • First, make it clear java -jar What did the order do , stay oracle A description of the command was found on the official website :

    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.

  • Show me my poor English translation again :

  1. Use -jar When parameters are , The following parameter is jar file name ( In this case, it is springbootstarterdemo-0.0.1-SNAPSHOT.jar);
  2. The jar The file contains class And resource files ;
  3. stay manifest In file Main-Class The definition of ;
  4. Main-Class The source code of the entire application specifies the startup class ;(in its source code)

To sum up :
java -jar Will go to jar Medium manifest file , Find the real startup class in there ;

exploration springbootstarterdemo-0.0.1-SNAPSHOT.jar

  1. springbootstarterdemo-0.0.1-SNAPSHOT.jar It's in front. springboot The result of the construction of the project , It's a compressed package , You can decompress with common compression tools , My environment here is MacBook Pro, use unzip To extract the ;
  2. There is a lot of content after decompressing , Let's focus on manifest dependent , In the red box below is manifest file :
     Insert picture description here
  3. Open the file in the red box above , The contents are as follows :
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idxImplementation-Title: springbootstarterdemoImplementation-Version: 0.0.1-SNAPSHOTStart-Class: com.bolingcavalry.springbootstarterdemo.Springbootstarter demoApplicationSpring-Boot-Classes: BOOT-INF/classes/Spring-Boot-Lib: BOOT-INF/lib/Build-Jdk-Spec: 1.8Spring-Boot-Version: 2.3.1.RELEASECreated-By: Maven Jar Plugin 3.2.0Implementation-Vendor: Pivotal Software, Inc.Main-Class: org.springframework.boot.loader.JarLauncher
  1. It can be seen from the above Main-Class Value org.springframework.boot.loader.JarLauncher, This and the front java Official documents correspond to , It's this JarLauncher The actual startup class is specified in the code of the class ;

Doubts arise

  1. stay MANIFEST.MF There's a line in the file :
Start-Class: com.bolingcavalry.springbootstarterdemo.Springbootstarter demoApplication
  1. Ahead java In official documents , Only mentioned Main-Class , Not mentioned Start-Class;
  2. Start-Class The value of is SpringbootstarterdemoApplication, This is ours java The only class in the code , Only the real application startup class ;
  3. So the question is : In theory , perform java -jar On command JarLauncher Class will be executed , But it's actually SpringbootstarterdemoApplication Carried out , What happened to this ?

guess

Guess before you do it , I think the reasons should be as follows :

  1. java -jar Command will start JarLauncher;
  2. Start-Class It's for JarLauncher With ;
  3. JarLauncher according to Start-Class eureka SpringbootstarterdemoApplication, And then execute it ;

analysis JarLauncher

  1. To download SpringBoot Source code , I downloaded it 2.3.1 edition , Address :https://github.com/spring-projects/spring-boot/releases/tag/v2.3.1.RELEASE
  2. JarLauncher The project is spring-boot-loader, Find out first JarLauncher Inheritance relationship of , Here's the picture , so JarLauncher Inherited from ExecutableArchiveLauncher, and ExecutableArchiveLauncher Parent class of Launcher At the top , It's an abstract class :

 Insert picture description here
3. java -jar Execution is JarLauncher Of main Method , as follows , Will instantiate a JarLauncher object , Then execute it launch Method , And bring all the input parameters into :

public static void main(String[] args) throws Exception { new JarLauncher().launch(args);}
  1. above launch Method in the parent class Launcher in :
protected void launch(String[] args) throws Exception { //  take jar The way to run after decompression is called exploded mode //  If it is exploded mode, Can't support the passage of URL load jar //  If not exploded mode, You can go through URL load jar if (!isExploded()) { //  If allowed to pass URL load jar, Register the corresponding processing class here  JarFile.registerUrlProtocolHandler(); } //  establish classLoader ClassLoader classLoader = createClassLoader(getClassPathArchivesIterator()); // jarmode Is to create docker The parameters used in the mirror image , This parameter is used to generate a file with multiple layer Mirror image of information  //  I don't care about jarmode String jarMode = System.getProperty("jarmode"); // without jarmode Parameters ,launchClass The value of comes from getMainClass() return  String launchClass = (jarMode != null && !j.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225090409045I.html

Scroll to Top