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

So you got a JAR file that contains your favorite Java game and you are wondering how to run a Java program from a JAR  file? Or you may be simply trying your hand after running the Java program from the command prompt and Eclipse IDE. Anyway, it's good to know How to execute Java programs from the JAR file, as the JAR file is the most popular way of shipping Java programs. JAR file usually contains all sources and resources in the form of class files, images, sound files, etc. 

In order to run a Java program from the JAR file, it must be an executable JAR file i.e. it manifest file inside the META-INF folder must contain a Main-class entry like Main-Class: Hello, which specifies 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 files 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 a Java program from an executable JAR file.

Running Java program from JAR file in Java

Suppose you have a hello.jar that contains class which has the main method as an entry point. When we run this JAR file using the 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 the Java program. If classpath is specified in the manifest file then that will be used by JVM and it will ignore the CLASSPATH environment variable, If you are not familiar with these details then see How classpath works Java, a 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 the manifest file doesn't contain the 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 runs 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 the command prompt in Windows 8 and UNIX:

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

That's all about how to run a JAR file in windows and UNIX. Running a JAR file is the simplest way to ship and run Java Program. You can also create a batch file to wrap the actual command, so that next time you can just run the batch file to start your Java program. 

Other Java programming tutorials you may find useful

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

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.