Jar file Vs Executable Jar file


Jar file is the combination of compiled java classes.

Executable jar file is also be combination of compiled java classes with Main class.

Normal jar file can be created as

C:\> jar -cvf TestNew.jar .

But while creating executable jar file we have to specify the Main-Class in a manifest file. Following are the steps to fallow while creating the executable jar file. Here its explained with a simple class in a test package.

Step1: create a java class TestNew under package test

package test;

public class TestNew{

public static void main(String a[]){

System.out.println(“Hello world”);

}

}

Step 2: compile the class with fallowing command

C:\>javac TestNew.java -d .

Note: Once you run the TestNew class you have to get “HelloWorld” in the console. To run use fallowing command

C:\>java test.TestNew

Step 3: Create a “mainClass” file in the directory where your test folder is located . This file contains a single line specifying where the main Class is to be found in the jar file. Note that I use the package specification. Here is the single line:

Main-Class: test.TestNew
Note: specify the single line only don't specify anything

Step 4: create the jar using fallowing command

C:\>jar cmf mainClass test.jar test

Where “c” represents the create that you are going to “create”, “m” specifies the manifest file mainClass (which adds information to the jar file on where the main class will be found) and “f ” refers to “file”.

Note: The file has to be created in your folder with the name test.jar

To view the content of the jar file you can use the fallowing command

C:\>jar tf test.jar

It displays as:

META-INF/

META-INF/MANIFEST.MF

test/

test/TestNew.class

To execute the jar file you can use the following commond

C:\>java -jar test.jar

o/p:

Hello world

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , ,
Posted in JAVA
2 comments on “Jar file Vs Executable Jar file
  1. Now this quite useful, since I am learning Java right now. Thanks for sharing

  2. HanuReddy says:

    excellent explanation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
May 2010
M T W T F S S
« Apr   Jun »
 12
3456789
10111213141516
17181920212223
24252627282930
31  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 284,400 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100