Using Java jar files. This tutorial explains the usage and purpose of Java JAR files.

1. Defining and using Java libraries

1.1. What is a Java library?

A Java library contains code which you can access and use in your Java project. The deployment format of a Java library is a JAR file.

1.2. What is a JAR file?

A JAR file is a Java archive based on the pkzip file format. JAR files are the deployment format for Java. A JAR can contain Java classes and other resources (icons, property files, etc.) and can be executable.

You can distribute your program in a jar file or you can use existing java code via jars by putting them into your classpath.

If you add a JAR file to your classpath, you can use its classes in your Java application.

2. Executable JAR

An executable JAR means the end-user can run the Java application without explicitly specifying the Java class which should be started. This is done via a MANIFEST.MF file which tells the JVM among other things which class contains the main() method and the classpath.

The following example shows one possible MANIFEST.MF file.

Manifest-Version: 1.0
Main-Class: com.vogella.example.MyApp
Class-Path:. lib/jcommon-1.0.6.jar lib/itext-1.4.6.jar

An empty line is required at the end of the MANIFEST.MF file.

You can create an executable JAR file via the following command.

jar -cvmf MANIFEST.MF app1.jar *.class

Eclipse provides an export wizard to create a JAR file which can be started via File  Export  Java  JAR file.

Create JAR file with the Eclipse export wizard

3. Links and Literature

Nothing listed.

If you need more assistance we offer Online Training and Onsite training as well as consulting