Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Creating an Executable Jar File


  • Please log in to reply
31 replies to this topic

#1 TALucas

TALucas

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts
  • Programming Language:Java, (Visual) Basic, Visual Basic .NET, Transact-SQL
  • Learning:C++, PHP, ActionScript

Posted 14 December 2008 - 07:35 PM

This is my first attempt at a tutorial here at CC, so if it's out of place, or I'm out of place, just let me know...;)

A Java Archive file (Jar) is a compressed file based on the ZIP format. Jar files can contain many files and folders, but most often hold Java .class files. This format helps keep all the files in one place, and reduce the overall size of a project. Another very useful function of Jar files is that some of them can be executed. The purpose of this tutorial is to explain how to create an executable Jar file.

First I’ll make the assumption that you have downloaded and installed the Java SE Development Kit (JDK), and have also set up the PATH and CLASSPATH environment variables. If you can run and compile .java programs from the command line, then you’re set to go.

First thing you need is a Java source file. A simple program that just gets and sends data to the command prompt will work as long as you run it from the command-line, but you will not be able to see any output if you just double click on the Jar file. So, in this tutorial I will use an example that creates a window using JFrame.

Step 1. Create a folder called test and put your Java source file in it. For my example I’ve created a simple clock application that just counts the number of days since something has happened…the number of days since you’ve quit a bad habit, the number of days your website has been up, for me it’s the number of days since I joined Code Call…13 days at the writing of this tutorial.

Posted Image

You can use the Clock.java example if you like or substitute one of your own. Go ahead and compile the source. You should now have a test folder that contains the source file, and two class files.

Posted Image

Step 2. Create a manifest file…I’ll call mine Clock.MF. A manifest is basically a file that contains information about the files packaged inside the Jar file. You can create yours using a text editor, just give it a .MF extension. Here’s what needs to go in this manifest file:

Manifest-Version: 1.0
Main-Class: Clock
Created-By: 1.2 (Sun Microsystems Inc.)

The first line tells us that this file conforms to 1.0 of the manifest specification. The second line tells which class inside the Jar file contains the “main” function. And the last line defines the version and the vendor of the java implementation on top of which this manifest file is generated.

Step 3. Create the Jar file using the jar command. In the example below (jar) is the name of the executable you call to create the Jar file. The jar command has a number of options…I used four of them (c –create the jar file, v –generates verbose output to the screen during file creation, f –specifies the file to be created, and m –indicates the manifest file to be used) After the options parameter comes the name of the jar file to be created (clock.jar), followed by the name of the manifest file to use (Clock.MF). The (*) wild card indicates to include all files in that directory. If you wanted you could use *.class to only include class files. Here's the command:

jar cvfm Clock.jar Clock.MF *

Your test folder should now contain five files…one of them being the new clock.jar file. You should be able to double click on the clock.jar, and it will execute. You can also execute this code from the command line by typing (java -jar clock.jar).

Posted Image

Attached Files


  • 2
Your thoughts are the architects of your destiny.

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 15 December 2008 - 06:33 AM

Excellent tutorial. Thank you for the read. +rep
  • 0

#3 Egz0N

Egz0N

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1155 posts

Posted 15 December 2008 - 10:46 AM

nice tutorial dude .. !! +rep :)
  • 0

#4 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 22 December 2008 - 05:37 AM

Sweet tutorial :)
  • 0

#5 TALucas

TALucas

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 118 posts
  • Programming Language:Java, (Visual) Basic, Visual Basic .NET, Transact-SQL
  • Learning:C++, PHP, ActionScript

Posted 22 December 2008 - 07:35 AM

Thanks guys.....

This is one of those topics I had to research on my own. Four years of college, and seems like they just taught you the basics.
  • 0
Your thoughts are the architects of your destiny.

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 22 December 2008 - 07:39 AM

One of the things everyone learns when they get out of college is that they only learned the basics. Most new graduates report that they learned more in the first 6 months of working than they did in 2-4 years of college. Of course, the college classes give a basis for learning in those first 6 months :)
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 whitey6993

whitey6993

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 156 posts

Posted 01 January 2009 - 12:30 PM

Awesome tutorial! Thanks for the post +rep
  • 0

#8 shoaibbi

shoaibbi

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 15 January 2009 - 07:55 AM

gud
  • 0

#9 dorothy

dorothy

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 January 2009 - 09:37 AM

nice tutorial! thx a lot!
  • 0

#10 anticafe

anticafe

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 20 February 2009 - 06:24 PM

Great tutorial. If you are making mobile game, and install that game via OTA (Over the Air), you must print many info of your game and mobile network here.

Step 2. Create a manifest file…I’ll call mine Clock.MF. A manifest is basically a file that contains information about the files packaged inside the Jar file. You can create yours using a text editor, just give it a .MF extension. Here’s what needs to go in this manifest file:


  • 0

#11 onetarget

onetarget

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 01 March 2009 - 08:36 AM

thanks very much , that helps me very much in my project
  • 0

#12 serka15

serka15

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 21 March 2009 - 02:57 AM

iznice :)

izverynize :)

Edited by WingedPanther, 21 March 2009 - 04:23 AM.
Double post

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download