
Jar Files
What do we need to start creating a jar file?
Prefer a graphical java program (GUI), so you can interact with the program.
The first step....
For beginners, it's better to create a new folder for the java file that we want to convert.
Then open the Command Prompt, now go to the directory of the java file
The second step....
Now, Locate the bin folder.
Often time it is in, c:\Program Files\Java\jdk1.6.0_11\bin
The third step....
Now use any text editor, and create Main-Class: Name of the class the main is in
Then save into the same director of the java file and .class file
The Last step....
Finally, we can create the jar file using this code
jar cvfm name of the jar name of the txt *.class
Now here is an example of creating the jar file.
Here are the key terms:
- c create new archive
- t list table of contents for archive
-x extract named files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-O store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
Example: PushButton.java

Here when we compile the file, we have 3 different classes.
Now using a text editor to create a text file, remember we need to define which class the main method is in.
Here the main is inside the PushButton.class

Here all we need inside the text file is Main-Class: PushButton
Note: We always need "Main-Class: "
PushButton is the class where we have the main ( Public static void main() )
Also, look at the picture, notice how we have a " Vertical bar under the Main-Class:" it is just an extra space that we always need
If we don't have that extra space, the text file would not work
Now, in the Command Prompt here are some keywords we should know
- cd means going to a director
- cd.. going back to the previous directory
- dir shows the director
Those three are the only keywords we need to know as for now for creating the jar file
Now open your command window, using the cd keyword to get to the directory where you have all the files.
Here, all my files are inside the folder j.
Then once I get to the folder, I type this in path c:\Program Files\Java\jdk1.6.0_11\bin; %path% that is the directory where I installed my jdk.
You might have installed into a different place, so make sure you got the right directory for your jdk or else it would gives you an error.
So the following is what you need to do when you have everything ready.
C:\Users\Jay\Desktop\j> path c:\Program Files\Java\jdk1.6.0_11\bin; %path%
<Push Enter>
C:\Users\Jay\Desktop\j> jar cmf mainClass.txt pushbutton.jar *.class
<Push Enter>
Now, the jar file is complete and will be locate in the same directory as the java file and .class file.
Hence, the program will be able to open with a double click just like an application.