The term JAR is an acronym built from the words: Java Application aRchive.
The term ‘archive’ is really key to understanding what a JAR file is. The JAR file is every file required to support an application zipped up and compressed into a single, archive file.
The Java JAR file uses a standard compression algorithm used by all of the most popular tools. A Java JAR file is really just a ZIP file with a .jar extension.
Functions with JAR file in Java: The basic functions of java JAR file are creating, updating, viewing and extracting. Let us look at them one by one.
1. In order to create a JAR file, we have to use the following
syntax :
jar -cf <jar_package_name> <original_package_name> .
The cf command means create-file. This creates a jar file of the package we are in currently.
2. In order to view the contents of a JAR file in table view, we use
syntax:
jar -tf <jarfilename>.jar.
While displaying you may see the manifest file. So what is it? The manifest file contains the information regarding other files in the archive.
3. If we need to update the contents of the JAR file, we can use
jar -uf <jarfilename>.jar (input_files)
4. When we need to extract a file with the help of a JAR file, we use jar -xf <jarfilename>.jar.
This action will create a MANIFEST file and a package containing the classes.
5. If we need to run a JAR file, we can use the command
java -jar <jarfilename>.jar
Example:
Step 1 : create 3 files named by A.java B.java C.java
// A.java
package ram;
import sita.B;
import rk.C;
public class A
{
public static void main(String[] a)
{
System.out.println("This is A class main method");
B.m1();
System.out.println("The value of integer variable is :"+C.a);
}
}
import sita.B;
import rk.C;
public class A
{
public static void main(String[] a)
{
System.out.println("This is A class main method");
B.m1();
System.out.println("The value of integer variable is :"+C.a);
}
}
//B.java
package sita;
public class B
{
public static void m1()
{
System.out.println(" This is B class main method");
}
public class B
{
public static void m1()
{
System.out.println(" This is B class main method");
}
}
//C.java
package rk;
public class C
{
public static int a=143;
}
public class C
{
public static int a=143;
}
Step 2:Use the following list of commands to create jar file
ubuntu@ubuntu:~/Desktop/pack$ gedit A.java
ubuntu@ubuntu:~/Desktop/pack$ gedit B.java
ubuntu@ubuntu:~/Desktop/pack$ gedit C.java
ubuntu@ubuntu:~/Desktop/pack$ javac -d . B.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java sita
ubuntu@ubuntu:~/Desktop/pack$ javac -d . C.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java rk sita
ubuntu@ubuntu:~/Desktop/pack$ javac -d . A.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java ram rk sita
ubuntu@ubuntu:~/Desktop/pack$ jar -cf kiran.jar ram
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java kiran.jar ram rk sita
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
ram/
ram/A.class
ubuntu@ubuntu:~/Desktop/pack$ jar -cf kiran.jar sita
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
sita/
sita/B.class
ubuntu@ubuntu:~/Desktop/pack$ jar -uf kiran.jar ram
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
sita/
sita/B.class
ram/
ram/A.class
Step 3: Now delete all the files such as A.java B.java C.java rk package,sita package, rk pakages filesubuntu@ubuntu:~/Desktop/pack$ gedit B.java
ubuntu@ubuntu:~/Desktop/pack$ gedit C.java
ubuntu@ubuntu:~/Desktop/pack$ javac -d . B.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java sita
ubuntu@ubuntu:~/Desktop/pack$ javac -d . C.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java rk sita
ubuntu@ubuntu:~/Desktop/pack$ javac -d . A.java
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java ram rk sita
ubuntu@ubuntu:~/Desktop/pack$ jar -cf kiran.jar ram
ubuntu@ubuntu:~/Desktop/pack$ ls
A.java B.java C.java kiran.jar ram rk sita
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
ram/
ram/A.class
ubuntu@ubuntu:~/Desktop/pack$ jar -cf kiran.jar sita
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
sita/
sita/B.class
ubuntu@ubuntu:~/Desktop/pack$ jar -uf kiran.jar ram
ubuntu@ubuntu:~/Desktop/pack$ jar -tf kiran.jar
META-INF/
META-INF/MANIFEST.MF
sita/
sita/B.class
ram/
ram/A.class
Step 4:
Now run the jar file
jar xf <jarfilename>
No comments:
Post a Comment