Skip to main content

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

ConceptDescription
Static ImportImports public static members
AmbiguityOccurs when same member exists in multiple classes
Normal ImportRequires class name to access members
Math.*Commonly used static import
JAR FileBundle of package s/classes
JAR FormatPlatform 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.p1
    • org.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

ConceptDescription
packageDefines namespace
import package.*Imports all accessible classes
Multiple package sCan be imported together
Public classesAccessible from other package s
JAR structureStores 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.jar file 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

ConceptDescription
jar -cfCreates JAR file
jar -cvfCreates verbose JAR
CLASSPATHJVM search path for classes/JARs
Executable JARJAR containing Main-Class
MANIFEST.MFStores metadata about JAR
Main-ClassEntry 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

OptionMeaning
cCreate JAR
vVerbose output
mInclude manifest file
fSpecify 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

OptionMeaning
xExtract
vVerbose
fFile name

Execution Flow

Application Flow

Important Notes

ConceptDescription
JOptionPaneUsed for GUI dialogs
Integer.parseInt()Converts String to int
Manifest FileStores Main-Class information
Executable JARRuns directly using java -jar
jar -cvmfCreates executable JAR with manifest
jar -xvfExtracts JAR contents