Run JAR File from Command Line
JAR is short for Java Archive. JAR files contain compressed Java project files. It includes the .class files and other metadata and resources needed for the project.
JAR files are a great way to share and distribute our Java code with others.
In this short tutorial, we will learn how to create and run a JAR file in Java.
Creating JAR Files
A JAR file is composed of the compiled Java .class files and other resources. We can create an executable or a non-executable JAR.
The difference between the two is that 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.
For this tutorial, we will work with just a single class whose main method prints Hello World.
public class HelloWorld
{
public static void main(String args[])
{
System.out.print("\n\nHello World\n\n");
}
}
Our directory has two files, the .java file and the manifest file which will be required for executable JAR.
We need the compiled .class files to create a JAR file. We first need to run the javac command to create the .class file from the .java file.
javac HelloWorld.java
Creating an Executable JAR File
To create an executable JAR file, we also need a manifest file. The content of the manifest file is shown below.
Main-Class: HelloWorld
Next, we can create a JAR file by running the following jar command.
jar -cfm HelloWorld.jar ManifestFile.txt HelloWorld.class
The -c flag is used to create an archive file. The -f flag is used to specify the file name. And the -m flag will include the content of the manifest file.
After executing the above command, a new JAR file with the name HelloWorld.jar is created in our current directory. Note that the .class file shown in the image below was created after running the javac command.
Creating a Non-Executable JAR File
To create a non-executable JAR file, we will exclude the -m flag. We don't need to pass the name of the manifest file to our command.
jar -cf HelloWorld.jar HelloWorld.class
Running JAR Files
Running the Executable JAR File
We can use the following java command with the -jar option to run the executable JAR file.
java -jar HelloWorld.jar
The output of the command is shown below.
Running the Non-Executable JAR File
Instead of using the -jar option, we will use the -cp option to run a non-executable JAR file. We need to specify the JAR file name and the main class name in the command.
java -cp HelloWorld.jar HelloWorld
The output of this command is shown below.
Running JAR Files with Command-Line Arguments
Any Java application can have any number of command-line arguments. These arguments are passed to the main() method of the class through a string array. JAR files also contain classes, and we can pass command-line arguments when running a JAR file.
The arguments should be separated by whitespace. If any argument contains whitespace, then we must enclose that argument in quotes.
Let's create a new class with a main method. The main()
method should print the command-line arguments passed to it.
public class CommandLineArgs
{
public static void main(String args[])
{
for(String s : args)
System.out.print(s);
}
}
We will compile the .java file and create the executable and non-executable jar files, as shown in the previous sections.
To run the executable JAR, we will use the java -jar command and pass the arguments.
In the command below, three arguments have been passed. Since the first two should contain whitespace at the end, we have enclosed them in quotes.
java -jar demo.jar "Welcome " "Back " User
For the non-executable JAR, we will use the -cf option and pass the main class name. We will also pass the arguments as we did in the code above.
java -cp demo.jar CommandLineArgs "Welcome " "Back " User
Summary
JAR files are a great way to share and distribute our code with others. In this tutorial, we learned how to create a JAR file and run it from the command line. We also learned how to pass command-line arguments to the main() method of the classes in JAR files.