JAR File : Java Archive File

Hello friends.

This is one of my tutorials for JAVA learner. All of you must be aware of Java Archive File i.e. JAR file.

Jar file is nothing but the collection of various class files into a single module.

Now one must be thinking that what is the actual need of jar file? Look friends this is not compulsory to create a jar file when we can directly run our code using class file to be interpreted. But what if we want that code to be used by any other person who does not know java or has no knowledge about how to run the java code? In this case we can provide the compressed version of class files into a single module called JAR file. Here in this case user does not have to do anything, he just has to double click the jar file and JVM will do all the remaining things.

For example if we are creating an application which takes input from user in any case (Either Upper or Lower) and the output must be just the opposite of the case inputed, then in such case running the class file each time wont be feel good for the user. So we can create the Jar file so that user don’t have to interpret class file each time.

Creating a jar File in Command Prompt

1) Start Command Prompt.

2) Navigate to the folder that holds your class files:

C:>JavaPrograms myworks

3) Set path to include JDK’s bin. (Ignore this if you have already set the path in Environmental Variables) For example:

C:JavaProgramsmywork> set path c:Program FilesJavajdk1.6.0_09bin;

This will set the Path.

4) Compile your classes just like we compile any other classes. Make sure that all the classes are compiled to avoid any Error.

5) Create a jar file:

C:JavaProgramsmywork> jar cf MyJar.jar  *.class  [<- This will include all the class files in the folder->]

C:JavaProgramsmywork> jar cf MyJar.jar .class files [<- Manually select the class files ->]

6) Then test your jar file using  java.exe as follows:

C:JavaProgramsmywork>java  MyJar.jar

This is all about Creating Java Archive Files, But what if I want to Modify, View Contents or Extract Contents from the Jar file. Following might help you in this.

1) View contents of a JAR file

C:JavaProgramsmywork>jar tf MyJar.jar

2) View contents with detail of a JAR file

C:JavaProgramsmywork>jar  tvf MyJar.jar

Note that we have used v (verbose) option to see the detail of JAR such as Date time etc.

3) Extract content of JAR file

C:JavaProgramsmywork>jar xf MyJar.jar

This will Extract all the files from the Current jar file.

4) Extract specific file from JAR file

C:JavaProgramsmywork>jar xf MyJar.jar Filename.class

5) Update a JAR file

C:JavaProgramsmywork>jar uf MyJar.jar addFile.class

Here addFile.class is any class file that we want to add to the MyJar.jar file

You can add more than one class files to the Existing jar file by placing them between White spaces.

ThankYou.

Regards-

Nikhil Naoghare.

3 thoughts on “JAR File : Java Archive File”

  1. Mary, I agree with your comment, this issues is a big deal but the book ‘tree house’ can help you understand the difference.

  2. hello
    i want to developp a jira plugin of migration
    and which consists in adding an interface in JIRA
    button ,browse ..
    the user can thus browse an excel file ;to convert it in a csv File (i have yet implemented the converter as a java class) and then to apply csv importer on this csv file
    would you please help me?can you provide me with tutoriels or ideas to perform this?or can i have a similar sample plugin with delailled steps of this?
    i’m really in urgent need (please send me your response by email)

Leave a Reply

Your email address will not be published. Required fields are marked *

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