JAR (Java Archive)
Points
- Package is the collection of classes whereas JAR is the collection of package s.
- JAR file format is platform-independent.
- You can compress and bundle multiple files associated with a Java application into a single file called a JAR file.
- JAR is based on ZIP algorithm.
JAR Syntax
jar-cf <NameOfJARFile> <Pack1> <Pack2> ...
jar-cvf <NameOfJARFile> <Pack1> <Pack2> ...
Example
jar-cf javaworld.jar
Static Import Ambiguity Flow
Important Notes
| Concept | Description |
|---|---|
| Static Import | Imports public static members |
| Ambiguity | Occurs when same member exists in multiple classes |
| Normal Import | Requires class name to access members |
Math.* | Commonly used static import |
| JAR File | Bundle of package s/classes |
| JAR Format | Platform independent |
Package Structure Example with Multiple Packages
Directory Structure
Student.java
package com.javaindia.p1;
public class Student {
public int sid;
public String sname;
public void show() {
System.out.println("Student -> show()");
System.out.println(sid + "\t" + sname);
}
}
Employee.java
package org.sd.p2;
public class Employee {
public int eid;
public String ename;
public void show() {
System.out.println("Employee -> show()");
System.out.println(eid + "\t" + ename);
}
}
Lab484.java
package com.javaindia.p3;
import com.javaindia.p1.*;
import org.sd.p2.*;
public class Lab484 {
public static void main(String args[]) {
Student st = new Student();
st.sid = 99;
st.sname = "Midhun";
st.show();
Employee emp = new Employee();
emp.eid = 88;
emp.ename = "Nivas";
emp.show();
}
}
Explanation
Imported Packages
import com.javaindia.p1.*;
importorg.sd.p2.*;
- Imports all accessible classes from:
com.javaindia.p1org.sd.p2
Objects Created
Student Object
Student st=new Student();
Employee Object
Employee emp=new Employee();
Expected Output
Student -> show()
99 Midhun
Employee -> show()
88 Nivas
Package Access Flow
Important Notes
| Concept | Description |
|---|---|
package | Defines namespace |
import package.* | Imports all accessible classes |
| Multiple package s | Can be imported together |
| Public classes | Accessible from other package s |
| JAR structure | Stores compiled package hierarchy |
Creating and Using JAR Files
Example Commands
D:\JavaWorld\MyJAR>
javac -d . *.java
jar-cf javaworld.jar com org
OR
javac -d . *.java
jar-cvf javaworld.jar com org
Compiling from Another Location
D:\JavaWorld\D1>
javac -d . Lab484.java
Result
Gives Error
Reason
- The
javaworld.jarfile is not available in the current location. - Therefore compiler/JVM cannot access package classes inside the JAR.
Setting CLASSPATH for JAR File
SETCLASSPATH=%CLASSPATH%;D:\javaworld\myjar\javaworld.jar;
After Setting CLASSPATH
javac -d . Lab484.java
Output
Compiles Successfully
Running the Program
java com.javaindia.p3.Lab484
Output
Runs Successfully
Creating the EXECUTABLE JAR File
Important Points
- When the JAR file is executable, you must specify the
Main-Class. - Main class information is stored inside
MANIFEST.MF. - When JVM executes the JAR, it searches for the main class in
MANIFEST.MF. - To specify manifest information, create a text file containing main class details.
Folder Structure
JAR Creation Flow
Important Notes
| Concept | Description |
|---|---|
jar -cf | Creates JAR file |
jar -cvf | Creates verbose JAR |
| CLASSPATH | JVM search path for classes/JARs |
| Executable JAR | JAR containing Main-Class |
MANIFEST.MF | Stores metadata about JAR |
| Main-Class | Entry point of executable JAR |
Lab485.java
package com.javaindia.p1;
import javax.swing.JOptionPane;
public class Lab485 {
public static void main(String[] args) {
String val1 = JOptionPane.showInputDialog("Enter first int Value");
String val2 = JOptionPane.showInputDialog("Enter second int Value");
int a = Integer.parseInt(val1);
int b = Integer.parseInt(val2);
int c = a + b;
String msg = "Sum of " + a + " and " + b + " is " + c;
JOptionPane.showMessageDialog(null, msg);
}
}
minfo.txt
Main-Class: com.javaindia.p1.Lab485
Compile the Java Class
javac -d . Lab485.java
Create the JAR File
jar-cvmf minfo.txt javaworld.jar com
Explanation of Options
| Option | Meaning |
|---|---|
c | Create JAR |
v | Verbose output |
m | Include manifest file |
f | Specify JAR file name |
Execute the JAR File
Method 1
java-jar javaworld.jar
Method 2
- Double click the executable JAR file.
Extract JAR File
jar-xvf javaworld.jar
Explanation
| Option | Meaning |
|---|---|
x | Extract |
v | Verbose |
f | File name |
Execution Flow
Application Flow
Important Notes
| Concept | Description |
|---|---|
JOptionPane | Used for GUI dialogs |
Integer.parseInt() | Converts String to int |
| Manifest File | Stores Main-Class information |
| Executable JAR | Runs directly using java -jar |
jar -cvmf | Creates executable JAR with manifest |
jar -xvf | Extracts JAR contents |