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, also known as the 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 runtimes, while also providing several key benefits such as enhanced security, compression for faster downloads, support for package sealing and versioning, and portability. Furthermore, JAR files can also be utilized to package Java extensions.

In this article, we'll guide you through the process of creating a simple Java application and then demonstrate how to assemble it into a JAR file, making it ready for distribution. Finally, we'll show you how to run the JAR file from the Linux terminal using the java command line tool and the -jar flag. It's crucial to note that you will need to have the java command line tool installed, as well as the -jar flag to execute the program encapsulated in the JAR file. When this flag is utilized, the specified JAR file becomes the source of all user classes and any other class path settings are ignored.

  • To begin, we'll start by crafting a basic Java class that contains a main method. This Java class will serve as an example to demonstrate the steps involved in creating and executing a .jar file in the Linux terminal. For this illustration, we'll name our Java application "MyApp".

$ vim MyApp.java
  • Incorporate the following code into the MyApp.java file.

public class MyApp{
	public static void main(String[] args){
		System.out.println("My Java Applicatoion");
	}
}
  • After compiling the Java source code, the next step is to package it into a JAR file with the aid of both javac and jar utilities, as outlined in the proceeding steps.

$ javac -d . MyApp.java
$ ls
$ jar cvf myapp.jar MyApp.class
$ ls

OR

javac MyApp.java
jar cf myapp.jar MyApp.class
chmod +x myapp.jar
java -jar myapp.jar
  • Now that the myapp.jar file has been generated, it's ready to run using the simple 'java' command, as demonstrated in the following step.

$ java -jar myapp.jar

no main manifest attribute, in myapp.jar

The error encountered in the previous step 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, which is necessary to run the application. To resolve this error, it's essential to add a Main-Class attribute to the manifest file that specifies the main class of the application. This line should be in the form "Main-Class: classname", where classname is the fully qualified name of the main class. By including this information in the manifest, the JVM will know exactly where to find the main method, allowing the application to run smoothly.

  • The solution to the error lies in updating the JAR archive to include a manifest attribute along with the code. This can be accomplished by creating a manifest.mf file, which will serve as a blueprint for the JAR file.

$ vim manifest.mf

"In order to add the necessary manifest attribute to the JAR file, simply copy and paste the specified line into the manifest.mf file."

Main-Class:  MyApp

With the manifest.mf file in place, it's now time to integrate it into the myapp.jar. This can be done by executing the following command, which will include the manifest.mf file into the archive.

$ jar cvmf manifest.mf myapp.jar MyApp.class
  • Upon executing the updated JAR file, the desired result should now be produced and displayed in the output, marking the successful resolution of the issue.

$ java -jar myapp.jar

My Java App

For a deeper understanding, consult the manual pages for java, javac, and jar commands.

$ man java
$ man javac
$ man jar

Conclusion

To summarize, the JAR file format offers a practical solution for bundling multiple Java class files, resources, and metadata into a single, platform-agnostic file for distribution. With the aid of the java command line tool and the -jar flag, you can effortlessly deploy a complete Java application and enjoy its advantages, including security, compression, and portability. By following the steps outlined in this guide, you now possess a strong understanding of how to create and run a JAR file in the Linux terminal. This knowledge will allow you to easily distribute and deploy your Java applications with confidence.

Updated on: 27-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements