How to Create and Execute a .Jar File in Linux Terminal

A JAR (Java ARchive) is platform-independent file format used to aggregate many Java class files and associated metadata and resources such as text, images, etc, into a single file for distribution.

It allows Java runtimes to efficiently deploy an entire application in one archive file, and provides many benefits such as security, its elements may be compressed, shortening download times, allows for package sealing and versioning, supports portability. It also supports packaging for extensions.

In this article, we will show how to create a simple Java application and bundle it into a JAR file, and demonstrate how to execute a .jar file from the Linux terminal.

To do this, you must have java command line tool installed to launche a Java application, and the -jar flag to execute a program encapsulated in a JAR file. When this flag is used, the specified JAR file is the source of all user classes, and other class path settings are ignored.

How to Create a JAR File in Linux

1. First start by writing a simple Java class with a main method for an application called TecmintApp, for demonstration purpose.

$ vim TecmintApp.java

Copy and paste the following code to TecmintApp.java file.

public class TecmintApp {
	public static void main(String[] args){
		System.out.println(" Just executed TecmintApp! ");
	}
}

Save the file and close it.

2. Next, we need to compile and pack the class into a JAR file using the javac and jar utilities as shown.

$ javac -d . TecmintApp.java
$ ls
$ jar cvf tecmintapp.jar TecmintApp.class
$ ls

3. Once tecmintapp.jar created, now you can excute the file using java command as shown.

$ java -jar tecmintapp.jar

no main manifest attribute, in tecmintapp.jar

From the output of the above command, we encountered an error. The JVM (Java Virtual Machine) couldn’t find our main manifest attribute, thus it could not locate the main class containing the main method (public static void main (String[] args)).

The JAR file should have a manifest that contains a line in the form Main-Class:classname that defines the class with the main method that serves as our application’s starting point.

4. To fix the above error, we will need to update the JAR file to include a manifest attribute together with our code. Let’s create a MANIFEST.MF file.

$ vim MANIFEST.MF

Copy and paste the following line to MANIFEST.MF file.

Main-Class:  TecmintApp

Save the file and let’s add the file MANIFEST.MF to our tecmintapp.jar using following command.

$ jar cvmf MANIFEST.MF tecmintapp.jar TecmintApp.class

5. Finally, when we executed the JAR file again, it should produce the expected result as shown in the output.

$ java -jar tecmintapp.jar

Just executed TecmintApp!

For more information, see the java, javac and jar command man pages.

$ man java
$ man javac
$ man jar

Reference: Packaging Programs in JAR Files.

That’s all! In this short article, we have explained how to create a simple Java application and bundle it into a JAR file, and demonstrated how to execute a .jar file from the terminal. If you have any questions or supplementary ideas to share, use the feedback form below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.