0% found this document useful (0 votes)
1 views5 pages

Jar File

The document explains how to create and run a JAR (Java Archive) file, which packages multiple Java class files and a manifest file specifying the main class. It outlines the steps for compiling Java code, creating a manifest file, and using the 'jar' command to create the JAR file. Additionally, it describes how to execute the JAR file both via command prompt and by double-clicking the file in a GUI environment.

Uploaded by

Kartik Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Jar File

The document explains how to create and run a JAR (Java Archive) file, which packages multiple Java class files and a manifest file specifying the main class. It outlines the steps for compiling Java code, creating a manifest file, and using the 'jar' command to create the JAR file. Additionally, it describes how to execute the JAR file both via command prompt and by double-clicking the file in a GUI environment.

Uploaded by

Kartik Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

JAR File

Collection of .class files


CLASSes are build on compiling:
- Jar can contain thousands of classes but which class will be the main class.
- For this we need MANIFEST File(manifest.mf) and save in practiceOOPS).
- Write the program in file as : Main-class: JarDemo ( It is must to press enter else
not work)
- The Main-Class attribute specifies the class containing the main() method.
- This file contains the name of such class that contains main method, here among 2
classes it will be JarDemo and not JarDemo$1(Anonymous class)
- Now the jar file have 3 components: JarDemo.class, JarDemo$1.class and
Manifest.mf
- With these 3 files we have to create jar file:

Command to create Jar file:


> jar cvfm demo.jar Manifest.mf JarDemo.class JarDemo$1.class

Where demo.jar is tha name of created jar file. And then give the name of all
components-all classes and manifest file. Start telling what’s the manifest file followed
by all classes name
In cvfm,

c : Create a new JAR.


v: Verbose output (shows details/ view or update).
f: Specify the JAR file name.
m: Include the manifest file.
Result: A JAR file named demo.jar was created, containing demoJar.class and the
manifest.

How to Run the JAR


Now that the JAR is created, you can execute it directly using:

c:\practiceOOPS> java -jar demo.jar

It will execute the jar file and give ouput from the main file here JarDemo.
Now to execute same program without opening command prompt and writhing the
command, copy the demo.jar from original loction and paste anywhere eg. Destop.
From here, this executable file can be executed directly by just double clicking it.
Such application is used when needed to execute file in GUI mode without writing
command and opening command prompt.

LINK: https://www.youtube.com/watch?v=MDBAIqU3S80

PRACTICE PROGRAM
1-make a java file:
public class demoJar {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
2- save as demoJar.java and compile it
3- make a manifest file-manifest.mf and
write:
Manifest-Version: 1.0
Main-Class: demoJar
(Press enter to insert a new blank line)
4- make a jar file file as below:
C:\practiceOOPS>jar cvfm demo.jar manifest.mf demoJar.class
added manifest
adding: demoJar.class(in = 421) (out= 292)(deflated 30%)

Output Explanation
In cvfm,

c : Create a new JAR.


v: Verbose output (shows details/ view or update).
f: Specify the JAR file name.
m: Include the manifest file.

1. added manifest:
o The tool successfully included the manifest.mf file in the JAR.
o This manifest file typically contains metadata such as the Main-Class
attribute, which specifies the entry point for the program.
2. adding: demoJar.class (in = 421) (out= 292) (deflated 30%):
o adding: The demoJar.class file is being added to the JAR.
o in = 421: The original size of the demoJar.class file is 421 bytes.
o out = 292: The compressed size of the demoJar.class file within the JAR
is 292 bytes.
o deflated 30%: The JAR tool compressed the demoJar.class file, reducing
its size by 30%
How to Run the JAR
Now that the JAR is created, you can execute it directly using:

c:\practiceOOPS> java -jar demo.jar

It will execute the jar file and give ouput from the main file here JarDemo.
Now to execute same program without opening command prompt and writhing the
command, copy the demo.jar from original loction and paste anywhere eg. Destop.
From here, this executable file can be executed directly by just double clicking it.
Such application is used when needed to execute file in GUI mode without writing
command and opening command prompt.

Purpose of . Jar file


.jar = Java Archive.
A JAR (Java ARchive) file is used to package multiple Java files (like .class, images, libraries)
into one file, making it easy to share, distribute, and run Java programs.

Simple program on making and using jar


Example: Even or Odd Checker

Step 1: EvenOdd.java

import java.util.Scanner;
public class EvenOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even ");
} else {
System.out.println(number + " is odd ");
} } }

c:\OOPJ>javac EvenOdd.java
c:\OOPJ>java EvenOdd
Enter a number: 9
9 is odd

c:\OOPJ>java EvenOdd
Enter a number: 8
8 is even

Steps to make .jar


1. Compile it: javac EvenOdd.java
2. Create manifest.mf:
Manifest-Version: 1.0
Main-Class: EvenOdd

3. Make the JAR file:


jar cvfm evenodd.jar manifest.mf EvenOdd.class
4. Run the JAR:
java -jar evenodd.jar
Import and Static Import
Befre java version 4 there was no static import, came after 5th version.
Why there is a need for static import:

You might also like