Run Jar File From Command Line

Beginner

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:

  1. Create a file named ManifestFile.txt in the ~/project directory and add the following to the file:
    Main-Class: HelloWorld
  2. Create the executable jar file with the following command:
    jar -cfm ~/project/HelloWorld.jar ~/project/ManifestFile.txt ~/project/HelloWorld.class
    The -c flag is used to create an archive file. The -f flag is used to specify the file name. The -m flag 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.

  1. Create a new Java class named CommandLineArgs.java in the ~/project directory with the following code:

    public class CommandLineArgs {
        public static void main(String args[]) {
            for (String s : args) {
                System.out.println(s);
            }
        }
    }
  2. Compile CommandLineArgs.java:

    javac ~/project/CommandLineArgs.java
  3. Create an executable JAR file:

    jar -cfm ~/project/CommandLineArgs.jar ~/project/ManifestFile.txt ~/project/CommandLineArgs.class
  4. Run 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.

Other Tutorials you may like