1. Overview
Typically, every meaningful application includes one or more JAR files as dependencies. However, there are times a JAR file itself represents a standalone application or a web application.
We'll focus on the standalone application scenario in this article. Hereafter, we'll refer to it as a JAR application.
Every command is made up of three elements: the command itself, an argument which tells the command what resource it should operate on, and an option that modifies the output. So, for example, to move a file from one folder to another on your Mac, you’d use the move command 'mv' and then type the location of the file you want to move. It's quite common to run applications from the command-line using arguments. Especially on the server-side. Usually, we don't want the application to do the same thing on every run: we want to configure its behavior some way. In this short tutorial, we'll explore how can we handle command-line arguments in Java. By default, the Toolbox App puts shell scripts in a directory from the system PATH environment variable, so you can run the name of the script as a command to launch WebStorm from any working directory. Command-line arguments. The launcher script accepts commands, options, and other arguments to modify its behavior.
In this tutorial, we'll first learn how to create a JAR application. Later, we'll learn how to run a JAR application with or without command-line arguments.
2. Create a JAR Application
A JAR file can contain one or more main classes. Each main class is the entry point of an application. So, theoretically, a JAR file can contain more than one application, but it has to contain at least one main class to be able to run.
A JAR file can have one entry point set in its manifest file. In this case, the JAR file is an executable JAR. The main class has to be included in that JAR file.
First of all, let's see a quick example of how to compile our classes and create an executable JAR with a manifest file:
A non-executable JAR is simply a JAR file that doesn't have a Main-Class defined in the manifest file. As we'll see later, we can still run a main class that's contained in the JAR file itself.
Here's how we would create a non-executable JAR without a manifest file:
3. Java Command Line Arguments
Just like any application, a JAR application accepts any number of arguments, including zero arguments. It all depends on the application's need.
This allows the user to specify configuration information when the application is launched.
As a result, the application can avoid hardcoded values, and it still can handle many different use cases.
An argument can contain any alphanumeric characters, unicode characters and possibly some special characters allowed by the shell, for example ‘@'.
Arguments are separated by one or more spaces. If an argument needs to contain spaces, the spaces have to be enclosed between quotes. Either single quotes or double quotes work fine.
Usually, for a typical Java application, when invoking the application, the user enters command-line arguments after the name of the class.
However, it's not always the case for JAR applications.
As we have already discussed, the entry point of a Java main class is the main method. The arguments are all Strings and are passed to the main method as a String array.
That said, inside the application, we can convert any element of the String array to other data types, such as char, int, double, their wrapper classes, or other appropriate types.
4. Run an Executable JAR with Arguments
Let's see the basic syntax for running an executable JAR file with arguments:
java -jar jar-file-name [args …]
The executable JAR created earlier is a simple application that just prints out the arguments passed in. We can run it with any number of arguments. Below is an example with two arguments:
We'll see the following output in the console:
So, when invoking an executable JAR, we don't need to specify the main class name on the command line. We simply add our arguments after the JAR file name. If we do provide a class name after the executable JAR file name, it simply becomes the first argument to the actual main class.
Most times, a JAR application is an executable JAR. An executable JAR can have a maximum of one main class defined in the manifest file.
Consequently, other applications in the same executable JAR file can't be set in the manifest file, but we can still run them from the command line just like we would for a non-executable JAR. We'll see exactly how in the next section.
5. Run a Non-Executable JAR with Arguments
To run an application in a non-executable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute:
java -cp jar-file-name main-class-name [args …]
As you can see, in this case, we'll have to include the main class name in the command line, followed by arguments.
The non-executable JAR created earlier contains the same simple application. We can run it with any (including zero) arguments. Here's an example with two arguments:
And, just like we saw above, we'll see the following output:
6. Conclusion
In this tutorial, we learned two ways of running a JAR application on the command line with or without arguments.
We also demonstrated that an argument could contain spaces and special characters (when allowed by the shell).
Mac Run App From Command Line With Arguments Windows 10
As always, the code for the examples is available over on GitHub.