Monday, December 6, 2010

Creating executable jar file in 3 easy steps

Jar stands for Java archive. It's nothing but a compressed zip file which contains all files and folders related to your java program whether it is image, package, class file or anything else.It helps programmer pack file in one file and reduce the size of project.A jar file can be executable.

Jar file can be created using a utility comes with jsdk called jar. You can find it in <Your Java sdk path>\bin folder as jar.exe in Windows.

Here is simple steps to create a jar file of your java project.

Suppose i have a source file called P3.java.

Now follow steps given below :

Step 1. Compile your java source into .class file. My class file is P3.class

Step 2. Now create a file called prg.MF known as manifest file that contains following entries using notepad.
    Created-By: 1.2 (Sun Microsystems Inc.)
    Manifest-Version: 1.0
    Main-Class: P3


    In above entries first line tells the version and the vendor of Java implementation.
    Second line indicates that our manifest file conforms to the specification version 1.0
    Third line describes a name of class file in which main function resides.

Step3.  Now the third and final step. Go to command prompt and execute jar utility as described below.
    jar cvfm Prog.jar prg.MF *
   
    Here c,v,f,m are different options of jar utility.
    c - Creates a new file
    v - Generates verbose output to screen (You can omit this option)
    f - Specifies archive file name
    m - Includes information from our own manifest file

    Prog.jar is our archive file name.You can specify any name.
    prog.MF is a manifest file created by us.
    * indicates that all file should be included in archive.
   
Check folder for generated Prog.jar and execute it.
   

2 comments:

  1. If you are using Eclipse than its better to use that for creating JAR. see here for step by step guide to create JAR form Eclipse

    ReplyDelete
  2. Yeah that's right but this steps is specially for students which are not allowed using Eclipse for Java programming.

    ReplyDelete