Running an executable JAR from command-line

An “executable JAR” is nothing but a JAR in which the entry point of execution is defined. That definition can be given in its manifest file (META-INF/

The general structure of a JAR file:


The manifest file should contain the details of the JAR file. It keeps the attribute as key-value pairs.

The structure of a manifest file:

Main-Class: com.package.MyJavaFile
Specification-Title: "My Classes"
Specification-Vendor: "Sun Microsystems, Inc."
Implementation-Version: "build01"
Implementation-Vendor: "Sun Microsystems, Inc."

If the executable JAR is dependent on any other JAR, we need to specify it in the Manifest itself (even though there is an option to do it while invoking the application thru java -cp).

Class-Path: aJar.jar anotherJar.jar

I had some problems in building a JAR file using the conventional jar -cvf command. Whatever I do, it was not picking my custom manifest (I’m very bad). So, I used ANT to build a JAR.

ANT file to build a JAR:

<project name="Demo" default="build.jar.file">
  <target name="build.jar.file">
    <jar destfile="myJar.jar" basedir=".">
        <attribute name="Main-Class" value="com.package.MyJavaFile" />
        <attribute name="Class-Path" value="aJar.jar anotherJar.jar" />
    <copyfile dest="/path/to/deploy" src="myJar.jar"/>
  • use ANT or Eclipse IDE to build the JAR

Running the JAR from the command-line:

$ java -jar myJar.jar [optional parameters]

External Links: – Packaging tutorial from Sun

About these ads