Vivek
Vivek

Reputation: 13228

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

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?

Upvotes: 21

Views: 4750

Answers (2)

shikjohari
shikjohari

Reputation: 2288

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

Upvotes: 2

icza
icza

Reputation: 417452

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

Upvotes: 25

Related Questions