Runtime JAR File in Java
JAR stands for Java Archive file. It is a platform–independent file format that allows bundling and packaging all files associated with java application, class files, audio, and image files as well. These files are needed when we run an applet program. It bundles JAR files use a Data compression algorithm. These jar files can be manipulated using zip programs like WINZIP or WINRAR.
It enables ease of distribution as all class files are packaged/bundled together and distributed to client applications. as one single jar file. These jar files can be signed by an author using digital certificate. These jar files can be authenticated using digital certificates and checking the author’s signature. The JRE (Java Runtime Environment) loads the classes from the JAR file without un-jarring it.
Methods: There are 2 ways of creating a JAR file.
- Using IDE
- Using the command line
Let us do discuss both of them in depth.
Method 1 – Using IDE
1. The creation of a JAR file through an IDE like Netbeans or Eclipse is pretty straightforward. In File, we have an export option that helps us to export the java application as a JAR file. After this go to
File -> Export->Java-> JAR file
2. Now in a JAR file specification dialog, specify the resources/files to be included in the JAR file. The export destination is the location where the jar file is to be created.
3. Click on the Finish button
4. We then provide the location where we wish to have the jar file created.
5. It is pictorially depicted below in two snapshots which are as follows:
Method 2: Using the command line
Using the jar tool, we can create a jar file as below
cmd>> jar cvf jarfile inputfileDir1 inputfileDir2
- c – create a new jar file
- v – verbose mode which displays the messages while the jar file is created.
- f – bundles into a jar file specified by the parameter jarfile instead of standard output.
- inputfileDir1, inputfileDir2 – indicates the input files which are to be bundled together in the jar file.
Now we will be manifesting the file as this is a special file that is bundled in your JAR file. It has special metadata like main class name, version control, the digital signature of the author, the java version used for bundling the jar file. The file name is “MANIFEST.MF” and it is a part of the META-INF subdirectory. If this file is not provided during bundling of the JAR file, it gets created automatically. When we extract and open the jar file, we can view this file. It has the following details
The jar file can be run by the java application directly if has a manifest file with the header as Main-class. The Main-class header has the fully qualified name of the class which has the main(). This specifies the entry point of the application.
Illustration: Consider creating a JAR file with a manifest File called helloworld.MF
Manifest-Version : 1.0 Main-class : com.sample.test.HelloWorld
Keep a note here m indicates the manifest file to be included while bundling into a jar file
Note: Here we compile the java file ‘HelloWorld.java’ using javac command. This command compiles the .java file and creates a class file HelloWorld.class. We then create the jar file helloworld.jar using jar command.
Lastly now while running the JAR file we use the below command to run the created jar file. When we run the jar file, ‘Helloworld‘ class which has main() gets loaded by the JVM, and the code gets executed.