1. Introduction
A JAR file is an archive that contains the classes and resources of a Java application. We can either treat it as a library so that others use it as a dependency. Or we can run it as an application. In this quick tutorial, we're going to look at how we can run a JAR file from the command line.
2. Sample Application
Take a look at our sample application. It contains the Greeting class:
public class Greeting {
public static void main(String[] args) {
if (args.length != 2) {
throw new IllegalStateException("Please enter your name and city.");
}
final String name = args[0];
final String city = args[1];
System.out.printf("Hello %s from %s%n", name, city);
}
}
Greeting defines a static main method and expects two command-line arguments representing name and city.
3. Run an Executable JAR
Every JAR file contains a MANIFEST.MF file that resides in the META-INF directory. When the manifest file contains a Main-Class entry, we call it an executable JAR. The main class holds the main method and serves as the entry point to our application.
Now, we'll first create our executable JAR file registering Greeting as the main class. For this purpose, we'll configure the Maven Jar Plugin:
<build>
<finalName>greet</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.javabyexamples.java.jar.Greeting</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Remember that jar is the default packaging type for a Maven project and the Maven Jar Plugin is responsible for creating the final JAR file. With this configuration, we're just modifying the plugin so that it adds the Main-Class entry as com.javabyexamples.java.jar.Greeting to the manifest file. We're also naming the JAR file by setting build.finalName as greet.
After running mvn clean package, we acquire our jar file, greet.jar. Now, we'll run our application from the command line using the java command:
java -jar greet.jar john "Sunny Beach"
Here, the -jar flag specifies that the application is in JAR format. We then specify the JAR file and pass the program arguments for Greeting. Note that the arguments are separated with space. If the argument itself contains a space, we use double-quotes. When the application is launched, the runtime system passes the command-line arguments to the application's main method as a String array.
Then it outputs:
Hello john from Sunny Beach
4. Run a Non-Executable JAR
When the manifest file doesn't include the Main-Class entry, the runtime system can't know the entry point to the application. We can create such a non-executable JAR by removing the previous Maven Jar Plugin configuration since Maven produces a non-executable JAR by default.
If we try to run a non-executable JAR with the java -jar command, we get an error:
no main manifest attribute, in greet.jar
To remedy this problem, we must specify the main class explicitly in the command line:
java -cp greet.jar com.javabyexamples.java.jar.Greeting john "High Valleys"
In this command, the -cp flag defines the application classpath as greet.jar. Then we specify the main class, com.javabyexamples.java.jar.Greeting. Also, we pass the program arguments separating them with space.
As a result, it prints:
Hello john from High Valleys
5. Summary
In this tutorial, we've looked at how we can run a JAR file from the command line. We also learned the difference between an executable JAR and a non-executable JAR.
Lastly, the source code for all examples is available on Github.