How to Run a JAR file from Command Prompt - Windows and UNIX

So you got a JAR file which contains your favorite Java game and you are wondering how to run Java program from JAR  file? Or you may be simply trying your hand after running Java program from the command prompt and Eclipse IDE. Anyway, it's good to know How to execute Java program from JAR file, as JAR file is a most popular way of shipping Java programs. JAR file usually contains all source and resource in the form of class files, images, sound files etc. In order to run Java program from JAR file, it must be an executable JAR file i.e. it manifest file inside META-INF folder must contain a Main-class entry like Main-Class: Hello, which specify the name of Java class which contains standard main method in Java. Without Main-class entry, you can not run a JAR file in Java and if you try to run such jar file using jar command you will get the error "Failed to load Main-Class manifest attribute from HelloWorld.jar". If you are not very much familiar with JAR file and don't know How to create an executable JAR, See this step by step tutorial on How to create executable JAR in Java. In this tutorial, we will only focus on running Java program from an executable JAR file.

Running Java program from JAR file in Java

How to run Java program from JAR file in command promptSuppose you have a hello.jar which contains class which has the main method as an entry point. When we run this JAR file using JAR command, JVM will call this main method which is specified in its manifest file as shown below:

Manifest-version: 1.0
Main-Class: Hello

By the way, don't forget to set PATH and  Classpath in Java before running Java program. If classpath is specified in the manifest file then that will be used by JVM and it will ignore CLASSPATH environment variable, If you are not familiar with these details then see How classpath works in Java, must read for any beginner in Java.

Once you are sure that Java is installed in your machine, path and classpath are proper, just execute the following command and it will run your Java program from the JAR file itself:

$ java -jar Hello.jar

If you see the error "no main manifest attribute, in Test.jar" it means your JAR file is not a runnable JAR file i.e. it's manifest file doesn't contain Main-Class attribute.

$ java -jar Test.jar
no main manifest attribute, in Test.jar

In my case MANIFEST.MF file just contains one line:

Manifest-Version: 1.0

Once  I added the Main-Class attribute it run just fine as shown below:

$ java -jar Test.jar
before swapping x: 3
before swapping y: 3
after swapping x: 3
after swapping y: 3

Here are nice slides with all steps required to run a JAR file from command prompt in Windows 8 and UNIX:

How to run a JAR file from command prompt in Windows 8

If you face any issue related to Java, classpath and path while running your Java program from JAR file, please post here. If you don't know how to create executable JAR file then see this tutorial. That tutorial gives you step by step guide to making an executable JAR using Eclipse IDE. 

Other Java programming tutorial you may find useful

1 comment:

  1. Make a batch file like this ...

    @echo off
    c:\Windows\System32\java.exe -jar %1

    (or whatever path contains your current updated java.exe)

    name it JavaRun.bat
    and put it of course in System32
    then just type at the command line "JavaRun (name of your jar file)" and it executes
    That's it


Feel free to comment, ask questions if you have any doubt.