In this article we are going to elaborate the jar commands (comes with jdk package)

What is Jar & Jar tool?

Jar means Java archive. After compiling one or more java classes, jar represent all classes tater in a single deplorable file. JAR is a standard ZIP or Archive format of java byte codes.

To make this, jdk has jar command line tool. The jar tool combines multiple files into a single JAR archive file.

jar is a general-purpose archiving and compression tool, based on ZIP and the ZLIB compression format.

Syntax :

    jar [option] [jar/class name/file name] [parameters..]

Jar Options :

  • c : Creates new archive
  • t : List table of contents for a archive
  • x : Extracts named (or all) files from a archive
  • u : Updates existing archive
  • v : Generates verbose output on standard output
  • f : Specifies archive file name
  • m : Include manifest information from specified manifest file
  • e : Specify application entry point for stand-alone application bundled into an executable jar file
  • 0 : Stores only; (no compression)
  • M : Does not create a manifest file for the entries
  • i : Generates index information for the specified jar files
  • C : Changes to the specified directory and include the input file arguments. Its operation is similar to the -C option of the UNIX tar utility
  • J(option) : Its supports options from java command. See my this post to get to know about those options. Basically , it pass option into the Java runtime environment.

Note : A manifest file entry named META-INF/MANIFEST.MF is automatically generated by the jar tool and is always the first entry in the jar file. Here is Jar specification.

Examples :

Add a index to jar file :

jar i [myJarFile] -J[option] 

Create a jar file :

  • Creates new jars(verbose output, store only and not creating manifest) specific file name, with change directory and a java option (including some files)

      jar c[v0M]f [myJarFile] [-C dir] [myInputfiles] -J[option]
    
  • Creates new jars(verbose output, store only),creating manifest, specific jar name, with change directory and a java option (including some files) and specify entry point(main class).

      jar c[v0]mf [manifest] [myJarFile] [-C dir] [myInputfiles] -J[option] [-e entrypoint]
    
  • Creates new jars(verbose output, store only and not creating manifest) with change directory and a java option(including some files)

      jar c[v0M] [-C dir] [myInputfiles] -J[option]
    
  • Creates new jars(verbose output, store only),creating manifest with change directory and a java option (including some files)

      jar c[v0]m [manifest] [-C dir] [myInputfiles] -J[option]
    

Extract jar file :

    jar x[v]f [myJarFile] [[myInputfiles]] -J[option]
    jar x[v] [[myInputfiles]] -J[option]

Update jar file :

    jar u[v0M]f [myJarFile] [-C dir] [myInputfiles] -J[option]
    jar u[v0]mf manifest [myJarFile] [-C dir] [myInputfiles] -J[option] [-e entrypoint]
    jar u[v0M] [-C dir] [myInputfiles] -J[option]
    jar u[v0]m manifest [-C dir] [myInputfiles] -J[option]

List table of contents of jar file :

    jar t[v]f [myJarFile] [[myInputfiles]] -J[option]
    jar t[v] [[myInputfiles]] -J[option]

Notes : Terms that i have used here

  • myJarFile : The file which will be created
  • myInputfiles : Files added to jar. (separated by space whilewriting)
  • manifest : Pre-existing manifest file whose name : value pairs are to be included in MANIFEST.MF in the jar
  • entrypoint : The name of the class that set as the application entry point

Thanks…:)