Introduction
In this lab, you will learn how to create and run a JAR file in Java on Ubuntu system. JAR is short for Java Archive. JAR files contain compressed Java project files, including the .class files and other metadata and resources needed for the project. We will cover how to create a jar file and execute it from the command line.
Create a Test Java File
In this step, you will create a Java file named HelloWorld.java in the ~/project directory. The content of HelloWorld.java should be:
public class HelloWorld
{
public static void main(String args[])
{
System.out.print("\n\nHello World\n\n");
}
}
We will use this file to create jar files to run later on in the lab.
Compile the Java File
In this step, you will compile the HelloWorld.java file using the following javac command:
javac ~/project/HelloWorld.java
Create an Executable JAR File
In this step, you will create an executable JAR file. An executable JAR file contains a manifest file, while the non-executable JAR file does not contain this. This manifest file contains the name of the class that should be executed when the JAR is run. On Ubuntu system, you can create an executable JAR file with this manifest file like this:
Create a file named
ManifestFile.txtin the~/projectdirectory and add the following to the file:Main-Class: HelloWorldCreate the executable jar file with the following command:
jar -cfm ~/project/HelloWorld.jar ~/project/ManifestFile.txt ~/project/HelloWorld.classThe-cflag is used to create an archive file. The-fflag is used to specify the file name. The-mflag is used to include the content of the manifest file.
Create a Non-Executable JAR File
In this step, you'll learn how to create a non-executable JAR file. We will exclude the -m flag. You don't need to pass the name of the manifest file to our command. You can create a non-executable jar file using the following command:
jar -cf ~/project/HelloWorldNonExec.jar ~/project/HelloWorld.class
Run the Executable JAR File
In this step, you will learn how to run the executable JAR file you created in Step 3. You can run it using the following command:
java -jar ~/project/HelloWorld.jar
The output of the command will be:
Hello World
Run the Non-Executable JAR File
In this step, you will learn how to run the non-executable JAR file you created in Step 4. You can run it using the following command:
java -cp ~/project/HelloWorldNonExec.jar HelloWorld
The output of the command will be:
Hello World
Run Jar Files with Command-Line Arguments
In this step, you will learn how to pass command-line arguments when running a JAR file.
Create a new Java class named
CommandLineArgs.javain the~/projectdirectory with the following code:public class CommandLineArgs { public static void main(String args[]) { for (String s : args) { System.out.println(s); } } }Compile
CommandLineArgs.java:javac ~/project/CommandLineArgs.javaCreate an executable JAR file:
jar -cfm ~/project/CommandLineArgs.jar ~/project/ManifestFile.txt ~/project/CommandLineArgs.classRun the following command to pass three command-line arguments:
java -jar ~/project/CommandLineArgs.jar "Welcome " "Back " User
The output of the command will be:
Welcome
Back
User
Summary
Congratulations! You have completed the Run Jar File From Command Line lab. You can practice more labs in LabEx to improve your skills.



