Create a JAR File
From this article, we can learn how to create a JAR file. A jar (Java archive) is a package file format commonly used to aggregate a maximum of Java class files and related metadata and resources (text, images, etc.) to distribute application software or libraries on the Java platform. Basically it can be used for tasks like lossless data compression, archiving, decompression, and archiving unpacking.
The basic essential format of the command for create a JAR file is:
jar cfm jar-file-name Manifest.txt input-file-name(s)/package-name(s)
JAR command options
The options and arguments used to creating a JAR file in this command are:
- The c option indicates that create a JAR file.
- The f option indicates that the output to go to a file rather than to stdout.
- The m option used to include manifest information from an existing manifest file given in the command as Manifest.txt.
- jar-file-name is the name that you want the resulting JAR file to have. You can use any filename to create a new JAR file. By convention, JAR filenames are given a .jar extension, though ‘.jar‘ the extension is not required to jar-file-name.
- The input-file(s)/package-name(s) argument is a space-separated list of one or more files or packages that you want to include in your JAR file. The input-file(s)/package-name(s) argument can contain the wildcard * symbol. If there are any “input-files” are directories, the contents of those directories will be added to the JAR archive recursively.
The c and f options can appear in either order, but there must not be any space between c and f like options.
This command will generate a compressed JAR file and place it in the current directory (Without any path given it will be stored inside the JDK/bin installation folder). The command will also generate a default manifest file for the JAR archive if we have not given it.
create a JAR file named MyNewJar.jar by entering the following command:
jar cfm MyNewJar.jar Manifest.txt MyClassesPackage/*.class
Here c option creates MyNewJar.jar file.
The m option includes Manifest.txt manifest command in a newly created jar manifest.MF file
MyClassesPackage is the packages we want to load classes into the jar file
By default it is created in JDK/bin installation folder