Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Home About eBooks Shop Donate Linux Online Courses Subscribe to Newsletter Linux Hosting

A-Z Linux Commands Get Involved

Linux Distro’s FAQ’s Programming Linux Commands Linux Tricks Best Linux Tools Certi�cations Guides

Monitoring Tools

Over 3,500,000+ Readers


How to Create and Execute a .Jar File in
Linux Terminal
Aaron Kili Last Updated: August 23, 2018 Linux Commands 1 Comment

A JAR (Java ARchive) is platform-independent �le format used to aggregate many Java class
�les and associated metadata and resources such as text, images, etc, into a single �le for
distribution.
A Beginners Guide To Learn
It allows Java runtimes to ef�ciently deploy an entire application in one archive �le, and Linux for Free [with Examples]
provides many bene�ts 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 �le,
Red Hat RHCSA/RHCE 8
and demonstrate how to execute a .jar �le from the Linux terminal.
Certi�cation Study Guide
[eBooks]
To do this, you must have java command line tool installed to launche a Java application, and
the -jar �ag to execute a program encapsulated in a JAR �le. When this �ag is used, the
speci�ed JAR �le is the source of all user classes, and other class path settings are ignored.
Linux Foundation LFCS and

How to Create a JAR File in Linux LFCE Certi�cation Study Guide


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

$ vim TecmintApp.java
Learn Linux Commands
and Tools

2 Ways to Re-run Last Executed


Commands in Linux
Copy and paste the following code to TecmintApp.java �le.
How to Find Out List of All Open
Ports in Linux
7 Quirky ‘ls’ Command Tricks
public class TecmintApp {
Every Linux User Should Know
public static void main(String[] args){
System.out.println(" Just executed TecmintApp! "); 14 Useful Examples of Linux
} ‘sort’ Command – Part 1
}
LFCA: Learn Basic Network
Troubleshooting Tips – Part 12
Save the �le and close it.
30 Useful ‘ps Command’
Examples for Linux Process
2. Next, we need to compile and pack the class into a JAR �le using the javac and jar utilities as
Monitoring
shown.

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

3. Once tecmintapp.jar created, now you can excute the �le 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 �nd our main manifest attribute, thus it could not locate the main class
containing the main method (public static void main (String[] args)).

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

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

$ vim MANIFEST.MF If You Appreciate What We Do


Here On TecMint, You Should
Consider:
Copy and paste the following line to MANIFEST.MF �le.

Main-Class: TecmintApp

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

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

5. Finally, when we executed the JAR �le 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 �le, and demonstrated how to execute a .jar �le from the terminal. If you
have any questions or supplementary ideas to share, use the feedback form below.

Commandline Tools

How to Install OpenSSL from Source in CentOS How to Limit User File Upload Size in Apache
and Ubuntu

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any
questions or doubts? do ask for help in the comments section.

You might also like