In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the jar in Springboot is like, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Summary:
Packaging with tools such as IDEA will lead to springboot-0.0.1-SNAPSHOT.jar,springboot-0.0.1-SNAPSHOT.jar.original, and then we'll find out what the connection is between them.
File comparison:
Enter the target directory, and the unzip springboot-0.0.1-SNAPSHOT.jar-d jar command unzips springboot-0.0.1-SNAPSHOT.jar to the jar directory
Enter the target directory, and the unzip springboot-0.0.1-SNAPSHOT.jar.original-d original command unzips springboot-0.0.1-SNAPSHOT.jar.original to the original directory
Springboot-0.0.1-SNAPSHOT.jar.original can not be executed, it will be repackage to generate springboot-0.0.1-SNAPSHOT.jar will become our executable fat jar, compared with the above files will find that executable fat jar and original jar directory is different, the most critical place is more than the org.springframework.boot.loader package, this is our usual java-jar springboot-0.0.1-SNAPSHOT.jar command startup secret. The contents of the MANIFEST.MF file contain a lot of key information
Manifest-Version: 1.0Start-Class: com.github.dqqzj.springboot.SpringbootApplicationSpring-Boot-Classes: BOOT-INF/classes/Spring-Boot-Lib: BOOT-INF/lib/Build-Jdk-Spec: 1.8Spring-Boot-Version: 2.1.6.RELEASECreated-By: Maven Archiver 3.4.0Main-Class: org.springframework.boot.loader.JarLauncher
I believe everyone can understand Main-Class: org.springframework.boot.loader.JarLauncher is the entry of our java-jar command, which will be analyzed later. Start-Class: com.github.dqqzj.springboot.SpringbootApplication is the entry main function of our program.
Springboot jar starts source code analysis public class JarLauncher extends ExecutableArchiveLauncher {static final String BOOT_INF_CLASSES = "BOOT-INF/classes/"; static final String BOOT_INF_LIB = "BOOT-INF/lib/"; public JarLauncher () {} protected JarLauncher (Archive archive) {super (archive) } / * to determine whether the file is archived or the directory of the file system, it can be guessed that it can be started based on the file system * / protected boolean isNestedArchive (Entry entry) {return entry.isDirectory ()? Entry.getName () .equals ("BOOT-INF/classes/"): entry.getName () .startsWith ("BOOT-INF/lib/");} public static void main (String [] args) throws Exception {/ * enter the parent initialization constructor ExecutableArchiveLauncher * launch method to Launcher to execute * / (new JarLauncher ()) .launch (args);}} public abstract class ExecutableArchiveLauncher extends Launcher {private final Archive archive Public ExecutableArchiveLauncher () {try {/ * uses the parent class Launcher to load resources, including classes of BOOT-INF and all archived files under lib * / this.archive = this.createArchive ();} catch (Exception var2) {throw new IllegalStateException (var2);}} protected ExecutableArchiveLauncher (Archive archive) {this.archive = archive } protected final Archive getArchive () {return this.archive;} / * get our application main function * / protected String getMainClass () throws Exception {Manifest manifest = this.archive.getManifest (); String mainClass = null; if (manifest! = null) {mainClass = manifest.getMainAttributes (). GetValue ("Start-Class") } if (mainClass = = null) {throw new IllegalStateException ("No 'Start-Class' manifest entry specified in" + this);} else {return mainClass;}} protected List getClassPathArchives () throws Exception {List archives = new ArrayList (this.archive.getNestedArchives (this::isNestedArchive)); this.postProcessClassPathArchives (archives); return archives;} protected abstract boolean isNestedArchive (Entry entry) Protected void postProcessClassPathArchives (List archives) throws Exception {} public abstract class Launcher {public Launcher () {} protected void launch (String [] args) throws Exception {/ * register the protocol processor, because Springboot is jar in jar, you have to rewrite the jar protocol to read the archive file * / JarFile.registerUrlProtocolHandler (); ClassLoader classLoader = this.createClassLoader (this.getClassPathArchives ()) / * this.getMainClass () is passed to the subclass ExecutableArchiveLauncher to implement * / this.launch (args, this.getMainClass (), classLoader);} protected ClassLoader createClassLoader (List archives) throws Exception {List urls = new ArrayList (archives.size ()); Iterator var3 = archives.iterator (); while (var3.hasNext ()) {Archive archive = (Archive) var3.next () Urls.add (archive.getUrl ());} return this.createClassLoader ((URL []) urls.toArray (new URL [0])) } / * this kind of loader is a key part of fat jar, because the traditional classloader cannot read the jar in jar model, so springboot implements its own implementation * / protected ClassLoader createClassLoader (URL [] urls) throws Exception {return new LaunchedURLClassLoader (urls, this.getClass (). GetClassLoader ()) } protected void launch (String [] args, String mainClass, ClassLoader classLoader) throws Exception {Thread.currentThread (). SetContextClassLoader (classLoader); this.createMainMethodRunner (mainClass, args, classLoader). Run ();} / * create application main function runner * / protected MainMethodRunner createMainMethodRunner (String mainClass, String [] args, ClassLoader classLoader) {return new MainMethodRunner (mainClass, args) } protected abstract String getMainClass () throws Exception; protected abstract List getClassPathArchives () throws Exception; / * get our archive file for launching jar * / protected final Archive createArchive () throws Exception {ProtectionDomain protectionDomain = this.getClass () .getProtectionDomain (); CodeSource codeSource = protectionDomain.getCodeSource (); URI location = codeSource! = null? CodeSource.getLocation () .toURI (): null; String path = location! = null? Location.getSchemeSpecificPart (): null; if (path = = null) {throw new IllegalStateException ("Unable to determine code source archive");} else {File root = new File (path); if (! root.exists ()) {throw new IllegalStateException ("Unable to determine code source archive from" + root) } else {return (Archive) (root.isDirectory ()? New ExplodedArchive (root): new JarFileArchive (root);} public class MainMethodRunner {private final String mainClassName; private final String [] args; public MainMethodRunner (String mainClass, String [] args) {this.mainClassName = mainClass; this.args = args! = null? (String []) args.clone (): null;} / * the final method can be found to be called by reflection of our application's main function * / public void run () throws Exception {Class mainClass = Thread.currentThread (). GetContextClassLoader (). LoadClass (this.mainClassName); Method mainMethod = mainClass.getDeclaredMethod ("main", String [] .class) MainMethod.invoke ((Object) null, this.args);}
Summary:
Too much content, not involving archiving files, protocol handlers, packaged war can also be started with commands, etc.
Org.springframework.boot spring-boot-loader
IDEA configures the startup class
About how the jar in Springboot is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.