Run JAR File in Command Prompt



A JAR file is like a ZIP file but for Java code. For the packaging of multiple class files, Java provides a file format known as JAR (Java Archive). Typically, a JAR file contains .class files, images, text files, and libraries in a single file that are required to execute the Java application or library.

This file format is used to distribute application software and libraries in Java. All the predefined libraries are available in this format.

If you have any library in JAR format, to use it in your application, you either need to place it in the current folder of the project or you need to set the class path for that particular JAR file.

Creating a Jar file

Java provides the jar command to work with jar files if you execute it in the command prompt, you will get the execution syntax and options of this command as shown below -

C:\>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

Example

Create a Java file with the name Sample.java in the path D:/example, copy and paste the following program into it -

public class Sample {
   public static void main(String args[]){
      System.out.println("Hi welcome to Tutorialspoint");
   }
}

Save the above file as sample.java. Make sure the class name matches the file name(sample.java), must have a public class sample. Otherwise, you will get a compilation error.

Compile the above program using the javac command as -

javac Sample.java

If your program gets executed without errors, a .class file will be generated in the current folder.

Create a Manifest File

A manifest file tells the JVM which class to run when executing the JAR. Without the ManifestFile, Java would not know the beginning of your application. Before creating the JAR, create a file named manifest.txt as given below -

Main-Class: Sample

The file must end with a blank line. Otherwise, the manifest File might not be read correctly.

Create the JAR File with Manifest

Use the below command to create your JAR file and include the manifest file -

jar cvfm sample.jar manifest.txt Sample.class

In the above Syntax, cvfm means -

  • c: Create new JAR

  • v: verbose(detail information)

  • f: Specify the JAR file name

  • m: include the custom manifest file

After adding the manifest file, when you run it, it will be -

added manifest
adding: Sample.class(in = 434) (out= 302) (deflated 30%)

NOTE: If you don't add a manifest file, Java will still create a default manifest, but it will not contain the Main-Class, which is necessary to run the JAR file. Use *.class if you have multiple compiled files to include in the JAR.

Run the JAR File

Once your JAR file is created, you can run it by using the following command prompt -

java -jar sample.jar

After running the above prompt, you will see -

Hi welcome to Tutorialspoint

In the syntax, java is the Java command-line tool, and -jar tells Java to run a JAR file and sample.  sample.jar is the name of your jar file. This command tells the Java Runtime Environment (JRE) to execute the application stored inside the sample.jar file.

Note: You can only use java -jar as we used above to run the JAR, if the JAR includes Main-Class. Otherwise, you will get an error.

Running a JAR without a Manifest File

If you don't add a manifest file with Main-class, and run it, you will get an error as below because Java doesn't know which class to run -

no main manifest attribute, in sample.jar

Running a JAR File with Arguments

If your Java program receives input through command-line arguments, you can pass them after the JAR file name as given below -

java -jar sample.jar argument1 argument2

Running a JAR file using eclipse

You can also create JAR files using IDE's. To create a JAR file using eclipse follow the procedure given below -

  • Open eclipse, create a project in it as -

  • Right Click on the project folder and select the Export option as ?

  • Under the Java category select JAR file.

  • Click on Next.

  • Enter the JAR file name and folder.

  • Click on Finish.

Updated on: 2025-04-21T15:21:33+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements