How to create jar file

Steps for creating jar file for application

JAR(Java ARchive ) file used to compress multiple classes into single file using ZIP format. In this tutorial we will see steps for creating jar file.

Let's assume our project having five files.

	   my project 
	     |--mainFrame.class
	     |--myclass.class
	     |--showclass.class
	     |--mycanvas.class
	     |--mypanel.class

Let's say mainFrame.class file is having main method in it. So for program execution this file has to be executed.

Step-1: Create manifest file

JAR file can contain multiple class files. One of these files is treated as main file ( which is having main method). This main file has to be specified in manifest file so that on time of execution main file can be identified.

Main-Class: mainFrame

Remember to give a new line at the end. Save this file with .txt extension. Let's say we have saved this file with name Manifest.txt

After : provide a space then write your class name.

Remember
  • Provide new line at the end of manifest file.
  • Save manifest file with .txt extension.
  • Give space after Main-Class: in first line.

Step-2 : Generate jar file

Now go to your project directory. For example let's assume project directory is on desktop with name my project.

C:\Users\jgyan> cd Desktop/my project
C:\Users\jgyan\Desktop\my project> 

now generate jar file using following command

C:\Users\jgyan\Desktop\my project> jar -cvfm project.jar Manifest.txt *

* is used to represent all files in directory. If you want to include only some file then at the place of * you need to provide name of class files with extensions.

Options
   c - Create Jar File
   f - Save generated file in project.jar file
   v - Show verbose output on stdout(terminal)
   m - Use given manifest file
 


Execution of jar file

jar file can be executed on double click or you can execute it using following command.

C:\Users\jgyan\Desktop\my project> java -jar project.jar