Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 899
Vivek Avatar asked Nov 07 '14 07:11

Vivek


People also ask

What happens when a JAR file is executed?

Does it extract the contents of the jar to a temp location and then executes the classes? The packaged data is extracted directly into memory and not stored temporarily on disk.

How do I extract a class from a JAR file?

Since JAR files work like ZIP files, you can use an archive program like WinRAR to extract them. For Windows users, you can install Java SE to use the 'jar' extraction command in the Command Prompt.

Can JAR file be extracted?

The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files. archived-file(s) is an optional argument consisting of a space-separated list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.

Does JAR file contain class files?

The JAR file contains the TicTacToe class file and the audio and images directory, as expected. The output also shows that the JAR file contains a default manifest file, META-INF/MANIFEST. MF, which was automatically placed in the archive by the JAR tool.


2 Answers

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
like image 161
icza Avatar answered Oct 22 '22 01:10

icza


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

like image 2
shikjohari Avatar answered Oct 22 '22 01:10

shikjohari