Jar File
Jar File
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,
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,
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:
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.
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