How does a jar file get executed? Do the classes get extracted somewhere?

≯℡__Kan透↙ 提交于 2019-11-29 12:26:59

问题


We know that jar is a compressed archive file format which acts as a container for compiled java classes and conf files. As far as I know, for reading any contents from a compressed container file, first they need to be extracted somewhere.

So how does the JVM execute classes inside the jar? Does it extract the contents of the jar to a temp location and then executes the classes?


回答1:


The JVM is capable of loading classes or files from a jar file without extracting the jar to temp files.

This functionality is also available to you in the standard library, see the JarFile for more information.

So no, the JVM does not extract a jar to temp files, classes (and resources) are simply loaded on demand.

A jar file is basically a zip file with a predefined entry "META-INF/MANIFEST.MF" (this is only mandatory in case of an executable jar). This MANIFEST.MF entry (file) contains some information read by the JVM. More on the Manifest files:

Working with Manifest Files: The Basics

In case of an executable jar the Manifest file also contains the main class that should be loaded and whose public static void main(String[]) method to be called in order to start the application. The Main-Class manifest entry specifies the main class:

Main-Class: classname



回答2:


No The jvm extracts the jar file to the memory and not to the file. It reads the MANIFEST.MF inside META-INF which has an entry for the main class. Jvm looks for the public static void main class inside this main class. This is how jvm finds the main class and executes the executable jar files



来源:https://stackoverflow.com/questions/26795976/how-does-a-jar-file-get-executed-do-the-classes-get-extracted-somewhere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!