JAR File Creation with Command Prompt: A Simple Tutorial

Computer GarageComputer Garage
2 min read

Overview

A JAR (Java Archive) is a package file format that compresses compiled class files, metadata, and resources (text, images, etc.) into one file for easy distribution. An executable Java program can be packaged into a JAR file. The manifest specifies the entry point of classes and an explicit classpath. Native launchers can be created on most platforms. Developers can also digitally sign JAR files, and the signature information will be included in the manifest file.

Creating a JAR

The Java JDK provides the jar command to work with JAR files. You can use a command prompt to run these commands.

Example:

    C:\Users\Admin>jar
    Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files...
    Options:
        -c  create 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  specify archive file name
        -m  include manifest information from specified manifest file
        -n  perform Pack200 normalization after creating a new archive
        -e  specify 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  do 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

Create a Java file with the following code snippet and save it as demo.java:


public class demo {
    public static void main(String []args){
        System.out.println("This is a demo file");
    }
}

Compile the above program using the following Java command:

javac demo.java

Once the class file is created, run the following command:

    C:\Users\Admin\Desktop>jar -cfvm demo.jar manifest.txt *.class
    added manifest
    adding: demo.class(in = 423) (out= 292)(deflated 30%)

Once the command is executed, the JAR file will be created in the same location with the specified name. Now, we can use the JAR file as needed.

0
Subscribe to my newsletter

Read articles from Computer Garage directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Computer Garage
Computer Garage

I'm a full stack web developer at a MNC Company. Majorly working with Angular and AWS. I love to learn new tech and trying over it.