From Juneday education
Jump to: navigation, search


This page contains a lecture on the Jar file format. We plan to also add lectures on the jar Java tool (for creating and using Jar files).


A file with an archive (like a zip file) of Java class files (and other files), which can be used by some application.

With a JAR file on the classpath, we can import packages and classes from the archive and use them in an application.

JAR files let us ship Java stuff in one file, rather than many files and many directories.

How to create a JAR file

Simplest way:

$ jar cvf some-file-name.jar some-directory
  • The cvf directive stands for Create Verbose File
  • some-file-name.jar will be created
  • some-directory contains the files to be included


We have a class Student, which we want to package in a JAR file:

└── se
    └── itu
        └── students
            └── domain
package se.itu.students.domain;
public class Student {

  private String name;
  private String email;

  public Student(String name, String email) { = name; = email;

  public String name() {
    return name;

  public String email() {
    return email;

  public String toString() {
    return new StringBuilder("Student with name <")
      .append(name).append("> and email <")


Compile the file to bin/

$ javac -d bin src/se/itu/students/domain/

The class file will end up here:

└── se
    └── itu
        └── students
            └── domain
                └── Student.class

Create the JAR file:

$ cd bin
$ jar cvf students.jar se
added manifest
adding: se/(in = 0) (out= 0)(stored 0%)
adding: se/itu/(in = 0) (out= 0)(stored 0%)
adding: se/itu/students/(in = 0) (out= 0)(stored 0%)
adding: se/itu/students/domain/(in = 0) (out= 0)(stored 0%)
adding: se/itu/students/domain/Student.class(in = 745) (out=
424)(deflated 43%)

The application using the JAR has a class using Student:

# Uses se.itu.students.domain.Student
# But where does it get it from?
package test;

import se.itu.students.domain.Student;

public class TestStudents {

  public static void main(String[] args) {
    Student[] students = {
      new Student("Henrik", ""),
      new Student("Rikard", ""),
      new Student("Aida", ""),
      new Student("Urban", "")

    for (Student stud : students) {


Compiling and running the application:

$ javac -cp .:students.jar test/
# On windows: javac -cp ".;students.jar" test/ (semicolon)

$ java -cp .:students.jar test.TestStudents
# Both compilation and running requires the JAR on the classpath!


Lecture videos

Lecture slides

Source code

Other pages on this Wiki about Jar

Further reading