Menu

Wednesday, June 1, 2011

Run an application which is packaged as a jar file

Step 1: Write a simple java class.
package com.jar;
public class HelloWorld {
    public static void main (String[] args) {
        System.out.println ("Hello World");    
    }
}

Step 2: Create a Manifest.txt file and add the main class which has "public static void main" method. The text file should be end with the new line otherwise the jar utility will not parse it properly. You will get this error - "Failed to load Main-Class manifest attribute from".

     Main-Class: fully qualified name of the class
     Ex: Main-Class: com.jar.HelloWorld

Step 3: Package this class as a jar file.

      > jar cfm HelloWorld.jar  Manifest.txt com*.*

      c means "create an archive file"
      f means "Specify the archive file name"
      m means "include the manifest information from specified manifest file"

Step 4: Run the jar file as below

     > java –jar HelloWorld.jar

     The out of the program
     >Hello World