fastjar Command in Linux



The fastjar command in Linux creates and manipulates the JAR files. The JAR stands for Java Archive. Since Java is a cross-platform programming language, it is easy to port JAR files from one system to another.

There are different tools to create a JAR file, such as Gradle or Maven. These tools are generally built into the IDEs. However, there are standalone command line tools as well, such as fastjar, gjar, and jar. In this guide, the fastjar command line utility will be discussed. It is a lightweight and easy-to-use command line tool for creating JAR files in Linux.

Table of Contents

Here is a comprehensive guide to the options available with the fastjar command −

Prerequisites to Use the fastjar Command

Before using the Linux fastjar command, ensure it is installed by checking its version −

fastjar --version
Prerequisites to Use fastjar Command

If it does not show the command version, install it using the instructions below.

To install the fastjar on Ubuntu, Debian, Debian-based distributions, and Kali Linux, use the following command −

sudo apt install fastjar

To install it on Arch Linux, use −

sudo pacman -S fastjar

Syntax of fastjar Command

The syntax of the Linux fastjar command is as follows −

fastjar [options] [jarfile] [manifestfile] [-C dir] files

In the above syntax −

  • [options] To specify options to create and modify the JAR file.
  • [jarfile] To specify the name of the jar file, e.g., myfile.jar.
  • [manifestfile] To specify the manifest file to be included in the JAR file. The manifest file contains JAR file metadata. If it is not specified the fastjar command automatically creates it.
  • [-C dir] To specify the base directory for the files to include in the JAR file.
  • files To specify the files or directories to include in the JAR file.

fastjar Command Options

The options used with the fastjar command are listed in the following table −

Flag Description
-c To create a new archive
-t To list the table of contents for archive
-x To extract specific or all file from the archive
-u To update the existing archive

The following parameters are optional and are generally used to manage the JAR file −

Parameters Description
-@ It reads the files from the standard input to include in the archive (used with -c or -u)
-C directory It is used to specify a directory to include files in the archive
-E It prevents the fastjar to read the contents from the specified directory, instead it reads the provided list of files
-M It disables the creation of manifest file
-i It generates the index of packages within the JAR file including the Class-Path
-J It ignores all the options started with -J
-0 It disables the zip compression (stores archive without compression)
-V / --version To display the command version
-f
-m manifest To include the manifest information from the specified file
-v It generates the verbose output
--help To display help related to command

Creating a JAR File using fastjar Command

To create a JAR file, a compiled Java class is needed. The compiled Java class is essentially a compiled Java program. Let’s create a simple Java program.

Use the nano editor to create a .java file −

sudo nano file.java

Now, write a Java program in the opened file.

class MyClass {
   public static void main(String[] args) {
      System.out.println("Hello Java");
   }
}
Cereate AK File Using fastjar Command 1

Save the file and exit the editor.

Next, create a manifest file. To create a manifest file, first create a directory with the name of META-INF.

sudo mkdir META-INF

Create a file with the name of MANIFEST.MF in the META-INF directory.

sudo nano META-INF/MANIFEST.MF

And add the following lines −

Manifest-Version: 1.0
Main-Class: MyClass
Cereate AK File Using fastjar Command 2

Save the file and exit the editor.

Finally, compile the Java program to generate the compiled class file −

javac file.java
Cereate AK File Using fastjar Command 3

Note − Before compiling the Java program file, ensure the JDK is installed.

The above command saves the compiled class in the current working directory as shown in the output image.

Now, create the JAR file using the Linux fastjar command −

fastjar -cvf file.jar META-INF MyClass.class
Cereate AK File Using fastjar Command 4

It will create a JAR file named file.jar in the current working directory as shown in the image.

To run the JAR file, use the java command with the -jar option and file name −

java -jar file.jar
Cereate AK File Using fastjar Command 5

To create an uncompressed JAR file, use the -0 option −

fastjar -cv0f file.jar META-INF MyClass.class

To create a JAR file using a specific manifest file, use the -m option with the manifest file name −

fastjar -cvmf file.jar MANIFEST.MF MyClass.class

Similarly, to create a JAR file without a manifest file, use the -M option −

fastjar -cvMf file.jar MyClass.class

In the above command, the -v option signifies the verbose.

Modifying a JAR File using fastjar Command

There are various options that can be used to modify the JAR file.

Listing the Contents of a JAR File

To list the contents of a JAR file, the -t option is used −

fastjar -tf file.jar
Listing Contents of JAR File

In the above command, the -f option is used to specify the JAR file name.

Extracting Contents from a JAR File

To extract contents from a JAR file, use the -x option −

fastjar -xvf file.jar
Extracting Contents from JAR File

Conclusion

The fastjar is a lightweight Linux command that is used to create and modify the JAR file. To create a JAR file, a compiled class is needed. If you want to create a JAR file with a specific manifest file, use the -m option. Moreover, the fastjar command can also be used to display and extract the contents of a JAR file.

To run the JAR file, use the java command with the -jar option and JAR file name.

In this tutorial, we explained the fastjar command, its installation, syntax, options, and usage in Linux.

Advertisements