In this tutorial, we are going to learn about what is a jar file and how to create and extract.

Jar File :

A jar file represents a group of .class files. In Java, if we need to work with several dependent classes it is not recommended to set classpath for every individual class.

We have to group all those .class files into a single unit called jar and then it will be set to the classpath.

Examples for jar Files :

  1. To develop a servlet based applications, all servlet dependent classes are available in servlet-api.jar, hence to compile a servlet program we have to place servlet-api.jar in the classpath.
  2. To use Log4J in our application all dependent classes are available in log4j.jar.

How to create Jar file :

We can create jar file by passing the below commands on your command prompt.

syntax :

jar   -<options>   jar_file_name   path(.class files location)

Example :

Usually, we can create a jar using below commands.

jar   -cvf   sample.jar   Test.class   Test2.class   Test3.class

jar   -cvf   sample2.jar   C:\Java\classes\*.class

Extract a jar File :

We can exact a jar file using the below command.

jar   -xvf   sample.jar

Display the content of a jar :

We can see the content of the jar without extracting it by using the below command.

jar   -tvf   sample.jar

Options :

Below are the all possible options to create a jar.

  •    -c  create a new archive
  •    -t  list table of contents for archive
  •     -x  extract named (or all) files from archive
  •     -u  update existing archive
  •     -v  generate verbose output on standard output
  •     -f specifies the archive filename
  •     -m  include manifest information from the specified manifest file
  •     -n  perform Pack200 normalization after creating a new archive
  •     -e specifies application entry point for stand-alone application bundled into an executable jar file
  •     -0  store only; use no ZIP compression
  •     -P  preserve leading ‘/’ (absolute path) and “..” (parent directory) components from file names
  •     -M does not create a manifest file for the entries
  •     -i  generate index information for the specified jar files
  •     -C  change to the specified directory and include the following file

Happy Learning 🙂