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.
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.
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:
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.Code:Manifest-Version: 1.0 Main-Class: Clock Created-By: 1.2 (Sun Microsystems Inc.)
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:
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).Code:jar cvfm Clock.jar Clock.MF *
![]()
Excellent tutorial. Thank you for the read. +rep
nice tutorial dude .. !! +rep![]()
Sweet tutorial![]()
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![]()
Awesome tutorial! Thanks for the post +rep
gud
VIvAcIoUs pAkIsTaNi
nice tutorial! thx a lot!
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:
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks