How to use JAR file in Java

To use JAR files in Java, first create a JAR file compressing and archiving all the classes of a program and then execute it wherever and whenever they want. JAR files makes the classes portable.

How to use JAR file in Java

To use JAR files in Java, first create a JAR file compressing and archiving all the classes of a program and then execute it wherever and whenever they want. JAR files makes the classes portable.

How to use JAR file in Java


JAR which stands for Java ARchive allows a programmer to archive or collect all the classes, resources, images, etc at one place so that it becomes easy to run a program.

It makes sure that the files remain secure. For this one has to digitally sign the JAR file. Users who want to use the secured file can check for this signature and know that such files can be trusted and then allow the access needed.

To run a program, a programmer needs lots of classes, if he/she do not use JAR file, then they will have to download every single file one at a time. This makes the work lengthy. But if you have archived all the classes in JAR files, then you just need to download one single file and Run it.

When you are making a JAR file, you are also compressing them for efficient storage.

JAR files also add portability as this file can be downloaded anywhere and run on any platform. All operating system support Java.

JAR file works as normal Zip file. You Zip a number of files i.e. compress and archive them at one and then Unzip them to use it.

Common commands and their operation:

  • Create a JAR file - jar cf jar-file input-file(s)
  • View the contents of a JAR file - jar tf jar-file
  • Extract the contents of a JAR file - jar xf jar-file
  • Extract specific files from a JAR file - jar xf jar-file archived-file(s)

To create a JAR file type "jar cf jar-file input-file(s)" in command prompt. "c" parameter means that you want to create a jar file and "f" parameter means that you want to send the output to a file. .jar extensions are given to JAR file.

To view a JAR file type "jar tf jar-file" in command prompt. "t" parameter means you want to view the table of contents of the JAR file and "f" parameter indicates the JAR file whose contents are to be viewed is specified on the command line.

To extract a JAR file type "jar xf jar-file" in command prompt. "x" parameter means that you want to extract files from the JAR archive and "f" parameter means that the JAR file from which files are to be extracted is specified on the command line.