Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Create and Execute a .Jar File in Linux Terminal?
Creating and executing a .jar file in Linux is a simple process that combines the compilation of Java source code and the packaging of class files and resources into a single file. With the terminal, you can easily compile your code, create a JAR file, make it executable, and run your Java application with ease.
The JAR format (Java Archive) offers a flexible and efficient way to package Java class files, resources, and metadata into a single, platform-agnostic file. The purpose of this format is to simplify the distribution and deployment process for Java applications, while also providing several key benefits such as enhanced security, compression for faster downloads, support for package sealing and versioning, and portability.
Creating a Simple Java Application
We'll start by creating a basic Java class with a main method. This will serve as our example application called MyApp.
$ vim MyApp.java
Add the following code to the MyApp.java file:
public class MyApp{
public static void main(String[] args){
System.out.println("My Java Application");
}
}
Compiling and Creating the JAR File
After writing the Java source code, compile it and package it into a JAR file using the javac and jar utilities:
$ javac MyApp.java $ jar cf myapp.jar MyApp.class $ ls
First Execution Attempt
Now let's try to run the JAR file:
$ java -jar myapp.jar
no main manifest attribute, in myapp.jar
Understanding the Manifest File
The error above highlights the importance of having a proper manifest in a JAR file. The JVM requires a main class to be specified, which acts as the entry point for the application. Without this information, the JVM cannot locate the main method in the JAR file.
To resolve this error, we need to create a manifest file that specifies the main class:
$ vim manifest.mf
Add the following line to the manifest.mf file (note the space after the colon):
Main-Class: MyApp
Important: The manifest file must end with a blank line, and there must be a space after the colon in "Main-Class: MyApp".
Creating JAR with Manifest
Now create the JAR file with the manifest included:
$ jar cvmf manifest.mf myapp.jar MyApp.class
The flags used are:
-
cCreate a new JAR file -
vVerbose output -
mInclude manifest information -
fSpecify JAR file name
Successful Execution
Now execute the updated JAR file:
$ java -jar myapp.jar
My Java Application
Alternative One-Step Approach
You can also create the JAR file with manifest in a single step:
$ javac MyApp.java $ echo "Main-Class: MyApp" > manifest.mf $ echo "" >> manifest.mf $ jar cvmf manifest.mf myapp.jar MyApp.class $ java -jar myapp.jar
Additional Commands
For a deeper understanding, consult the manual pages for the Java tools:
$ man java $ man javac $ man jar
Key Points
JAR files require a manifest with Main-Class attribute to be executable
The manifest file must end with a blank line
Use
jar cvmfflags to create JAR with manifestExecute JAR files with
java -jar filename.jarJAR files provide compression, portability, and easy distribution
Conclusion
Creating and executing JAR files in Linux involves compiling Java source code, creating a proper manifest file, and packaging everything together. The manifest file is crucial as it tells the JVM which class contains the main method. Once properly configured, JAR files provide an excellent way to distribute and deploy Java applications across different platforms.
