How to Create,View,Extract and Update Jar using Java

Today I am going to explain about JAR commands and it's various options. Using this tutorials, you will be able to :

  • Create JAR file
  • View Contents of jar file
  • display verbose output of jar file
  • Extract content of jar file
  • Modify manifest file of jar file
  • Update jar file
  • Run jar file

Following are list of Jar commands used for JAR Operations :

For create jar file, please refer to How to create JAR file

Jar command to view contents of JAR File
jar tf MyApplication.jar

How to display verbose output of JAR File
jar tvf MyApplication.jar

How to extract contents of JAR File
jar xf jar-file [archived-file(s)]



Description of Options :
* The x option - indicates that you want to extract files
* The f options - indicates that the JAR file from which files are to be extracted
* The jar-file - name or path of source jar file
* archived-file(s) - optional argument consisting of a space-separated list of the files to be extracted from the archive.

How to modifying a Manifest File
jar cmf manifest-addition jar-file input-file(s)

* The c option - indicates that you want to create a JAR file.
* The m option - indicates that you want to merge information from an existing manifest file into the manifest file of the JAR file you are creating.
* The f option - indicates that you want the output to go to a Jar file that you are creating rather than to stdout.
* manifest-addition is the name (or path and name) of the existing text file whose contents you want included in the JAR file's manifest.
* jar-file is the name that you want the resulting JAR file to have.
* The input-file(s) argument is a space-separated list of one or more files that you want to be placed in your JAR file.

How to update JAR File
jar uf jar-file input-file(s)

* The u option - indicates that you want to update an existing JAR file.
* The f option - indicates that the JAR file to update is specified on the command line.
* jar-file is the existing JAR file that's to be updated.
* input-file(s) is a space-deliminated list of one or more files that you want to add to the Jar file.

Ex.
jar uf MyApplication.jar PATH-OF-NEW-FILE

You can use the -C option to "change directories" during execution of the command. For example:
jar uf MyApplication.jar -C MY_DIRECTORY FILE_NAME

How to run Jar File
  1. If you have jar file included into Applets then use with
<applet code=MyAPP.class archive="applets/MyAPP.jar"
width=120 height=120>
</applet>

2.Run Java file using
java -jar jar-file

No comments:

Post a Comment