Creating and Executing a .jar File in Linux Terminal
Last Updated :
06 Jul, 2021
JAR – Java Archive. It is like a zip file but for java classes. It combines all the .class files in Java into a single .jar file. It is used to download all java classes on HTTP in a single operation. These can be created using the “jar” CLI tool. It also has an optional META-INF which can include files like –
- MANIFEST.MF – manifest file is used to define the extension and package-related data.
- INDEX.LIST – It contains location information for packages defined in an application or extension.
- x.SF – This is the signature file where ‘x’ is the base file name.
- x.DSA – This file stores the digital signature of the corresponding signature file.
- services/ – This directory stores all the service provider configuration files.
The most common and majorly used file is MANIFEST.MF
Requirements
Java (JDK + JRE) must be installed. Check by using command –
Java --version
jar --version
Create Jar files
Let us consider 4 class files – Class1, Class2, Class3, Class4
Java
class Class2 {
public static void cls2Method(){
System.out.println( " Hello from Class2 " );
}
}
class Class3 {
public static void cls3Method(){
System.out.println( " Hello from Class3 " );
}
}
class Class4 {
public static void cls4Method(){
System.out.println( " Hello from Class4 " );
}
}
public class Class1 {
public static void main(String[] args){
System.out.println( " Hello from Class1 " );
Class2.cls2Method();
Class3.cls3Method();
Class4.cls4Method();
}
}
|
Output
Hello from Class1
Hello from Class2
Hello from Class3
Hello from Class4
Let’s move them into one jar file called “allClasses.jar”.
Run the command:
jar –create –file allClasses.jar Class1.class Class2.class Class3.class
To get a clear output use –verbose
jar –create –verbose –file allClasses.jar Class1.class Class2.class Class3.class
Output:

This will create an allClasses.jar file in the folder. Let’s understand the above command thoroughly.
- –create: It is an option to create a jar file. We can perform more operations like extract, update, etc.
- –verbose: It gives a crisp and clear output and shows what’s going on behind the scenes.
- –file filename: filename is the name for the jar file. Extension(.jar) is optional.
- In the end, we specify the whole list of files to put in the jar file.
The shorthand for this command will be –
jar -cvf allClasses.jar *
Note: * represents all the files in the current folder. Use * with caution.
To update,
jar -uf allClasses.jar Class4.class
-u is for the –update.
This will update allClasses.jar files with the new Class4.class.
From the verbose output, it is clear that compression is taking place, to bypass compression or to archive files without compression use the option –no-compress.
jar –create –verbose –no-compress –file allClasses.jar Class1.class Class2.class Class3.class
or
jar -cvf0 allClasses.jar *
Output:

Execute Jar Files
Execution is fairly simple of jar files. Just use the command
java -jar allClasses.jar
If this gives an error – no main manifest attribute, in allClasses.jar
Open ./META-INF/MANIFEST.MF file and add a line in it.
Main-Class: classname
In our case, the class name will be “Class1” as it’s our main class.
Now, the file will look like this:-
Manifest-Version: 1.0
Created-By: Ubuntu
Main-Class: Class1
Again run the command
java -jar allClasses.jar
Output:
Hello from Class1
Hello from Class2
Hello from Class3
Hello from Class4
Caution: Don’t leave any space between lines in the MANIFEST.MF file else it will show the unexpected error.
If you still get the error and not able to find the error, use the below workaround –
java -cp allClasses.jar Class1
where Class1 is the name of the main class.
Note:
To Extract use command –
jar --extract --file allClasses.jar
or
jar -xf allClasses.jar
Similar Reads
How to Create a VIM File in Linux CMD
In Linux, there are many ways to open and create files, but Vim stands out as a powerful and versatile text editor. It comes pre-installed on most Linux systems and allows you to create, edit, and manage files directly from the terminal. In this article, we will explain you, how to create a new file
3 min read
How to Create an Executable JAR with Maven?
The Maven is a powerful build automation tool primarily used for java program projects. And It simplifies that build process, dependency management and project configuration through a standard project object model which means POM file. This file is heart of the Maven Project and Build Automation. In
4 min read
How to Execute a .class File in Java?
A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
1 min read
Internal and External Commands in Linux
The UNIX system is command-based i.e things happen because of the commands that you key in. All UNIX commands are seldom more than four characters long. They are grouped into two categories: Internal Commands : Commands which are built into the shell. For all the shell built-in commands, execution o
4 min read
How to Compile and Run C program in Terminal?
To efficiently compile and run C programs, users typically utilize a compiler or the built-in terminal. The command prompt (CMD) on Windows can be employed to process C programs and generate the desired outputs. To create an executable C program, users must compile their C code using the system term
6 min read
Compressing and Decompressing files using GZIP Format in java
The java.util.zip package provides classes compress and decompress the file contents. FileInputStream, FileOutputStream and GZIPOutputStream classes are provided in Java to compress and decompress the files. Compressing a File using GZIPOutputStream Methods used in the program read(): Reads a byte o
2 min read
Installing Java 14 in Ubuntu and Linux Mint
The JDK is a development environment for building applications using the Java programming language. The JDK includes tools useful for developing and testing programs. Here we will install the Java14 in Linux. Installing in Ubuntu: Here we are going to PPA repositories to install javajdk-14 on ubuntu
2 min read
Calling an External Program in Java using Process and Runtime
Java contains the functionality of initiating an external process - an executable file or an existing application on the system, such as Google Chrome or the Media Player- by simple Java code. One way is to use following two classes for the purpose: Process class Runtime class The Process class pres
2 min read
Shell Scripting - Creating a Binary file
While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or
4 min read
How to View the Content of File in Linux | cat Command
The cat command in Linux is more than just a simple tool, it's a versatile companion for various file-related operations, allowing users to view, concatenate, create, copy, merge, and manipulate file contents. Let's see the details of some frequently used cat commands, understanding each example alo
7 min read