How to run a JAR file through command prompt in java?

For packaging of class files Java provides a file format known as JAR (Java Archive). Typically, a JAR file contains .class files, images, text files, libraries that are required to execute the 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 this format to use it In your application either you need to place it in the current (or, lib) folder of the project or, you need to set the class path for that particular JAR file.

Creating a Jar file

Java provides 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 −

Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
   -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

You can create a JAR file using by executing this command with the options c, v, f Following is the syntax to create a JAR file using command prompt −

jar cvf jar_file_name output_path


Create a java file with name in the path D:/example, copy and paste the following program in it −

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

Compile the above program using the javac command as −


If your program gets executed without errors .class file will be generated in the current folder. Now, create a jar file for the generated class as −

C:\Sample>jar cvf sample.jar *.class
added manifest
adding: Sample.class(in = 434) (out= 302) (deflated 30%)
Once you execute a JAR file is generated with the specified name.

Creating 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: 14-Oct-2019

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started