---===[ Message from the authors: This wiki will be configured to use SSL - may be down 22.00-06.00 (Swedish time) during the period 2019-02-21 - 2019-02-23. Use wiki2.juneday.se during hose hours ===----

Java:Tools:Jar

From Juneday education
Jump to: navigation, search

Description

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).

Introduction

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

Example

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

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

  private String name;
  private String email;

  public Student(String name, String email) {
    this.name = name;
    this.email = email;
  }

  public String name() {
    return name;
  }

  public String email() {
    return email;
  }

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

}

Compile the file to bin/

$ javac -d bin src/se/itu/students/domain/Student.java

The class file will end up here:

bin/
└── 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:

test/
└── TestStudents.java
# 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", "henke@gu.se"),
      new Student("Rikard", "rille@gu.se"),
      new Student("Aida", "aida@gu.se"),
      new Student("Urban", "ubbe@gu.se")
    };

    for (Student stud : students) {
      System.out.println(stud);
    }

  }
}

Compiling and running the application:

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

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

Links

Lecture videos

Lecture slides

Source code

Other pages on this Wiki about Jar

Further reading