Java jar main class argument

java-run-jar-with-arguments

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.

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:

$ javac com/baeldung/jarArguments/*.java $ jar cfm JarExample.jar ../resources/example_manifest.txt com/baeldung/jarArguments/*.class

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.

Читайте также:  Первая строка в css

Here’s how we would create a non-executable JAR without a manifest file:

$ jar cf JarExample2.jar com/baeldung/jarArguments/*.class

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:

$ java -jar JarExample.jar "arg 1" [email protected]

We’ll see the following output in the console:

Hello Baeldung Reader in JarExample! There are 2 argument(s)! Argument(1):arg 1 Argument(2):[email protected]

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:

$ java -cp JarExample2.jar com.baeldung.jarArguments.JarExample "arg 1" [email protected]

And, just like we saw above, we’ll see the following output:

Hello Baeldung Reader in JarExample! There are 2 argument(s)! Argument(1):arg 1 Argument(2):[email protected]

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).

As always, the code for the examples is available over on GitHub.

Источник

Setting an Application’s Entry Point

If you have an application bundled in a JAR file, you need some way to indicate which class within the JAR file is your application’s entry point. You provide this information with the Main-Class header in the manifest, which has the general form:

The value classname is the name of the class that is your application’s entry point.

Recall that the entry point is a class having a method with signature public static void main(String[] args) .

After you have set the Main-Class header in the manifest, you then run the JAR file using the following form of the java command:

The main method of the class specified in the Main-Class header is executed.

An Example

We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.

We first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClass

When you run the JAR file with the following command, the main method of MyClass executes:

Setting an Entry Point with the JAR Tool

The ‘e’ flag (for ‘entrypoint’) creates or overrides the manifest’s Main-Class attribute. It can be used while creating or updating a JAR file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp :

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

If the entrypoint class name is in a package it may use a ‘.’ (dot) character as the delimiter. For example, if Main.class is in a package called foo the entry point can be specified in the following ways:

jar cfe Main.jar foo.Main foo/Main.class

Источник

The jar Command

The jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. Initially, the jar command was designed to package Java applets (not supported since JDK 11) or applications; however, beginning with JDK 9, users can use the jar command to create modular JARs. For transportation and deployment, it’s usually more convenient to package modules as modular JARs.

The syntax for the jar command resembles the syntax for the tar command. It has several main operation modes, defined by one of the mandatory operation arguments. Other arguments are either options that modify the behavior of the operation or are required to perform the operation.

When modules or the components of an application (files, images and sounds) are combined into a single archive, they can be downloaded by a Java agent (such as a browser) in a single HTTP transaction, rather than requiring a new connection for each piece. This dramatically improves download times. The jar command also compresses files, which further improves download time. The jar command also enables individual entries in a file to be signed so that their origin can be authenticated. A JAR file can be used as a class path entry, whether or not it’s compressed.

An archive becomes a modular JAR when you include a module descriptor, module-info.class , in the root of the given directories or in the root of the .jar archive. The following operations described in Operation Modifiers Valid Only in Create and Update Modes are valid only when creating or updating a modular jar or updating an existing non-modular jar:

All mandatory or optional arguments for long options are also mandatory or optional for any corresponding short options.

Main Operation Modes

When using the jar command, you must specify the operation for it to perform. You specify the operation mode for the jar command by including the appropriate operation arguments described in this section. You can mix an operation argument with other one-letter options. Generally the operation argument is the first argument specified on the command line.

-c or —create Creates the archive. -i= FILE or —generate-index= FILE Generates index information for the specified JAR file. -t or —list Lists the table of contents for the archive. -u or —update Updates an existing JAR file. -x or —extract Extracts the named (or all) files from the archive. -d or —describe-module Prints the module descriptor or automatic module name.

Operation Modifiers Valid in Any Mode

You can use the following options to customize the actions of any operation mode included in the jar command.

Changes the specified directory and includes the files specified at the end of the command line.

-f= FILE or —file= FILE Specifies the archive file name. —release VERSION

Creates a multirelease JAR file. Places all files specified after the option into a versioned directory of the JAR file named META-INF/versions/ VERSION / , where VERSION must be must be a positive integer whose value is 9 or greater.

At run time, where more than one version of a class exists in the JAR, the JDK will use the first one it finds, searching initially in the directory tree whose VERSION number matches the JDK’s major version number. It will then look in directories with successively lower VERSION numbers, and finally look in the root of the JAR.

-v or —verbose Sends or prints verbose output to standard output.

Operation Modifiers Valid Only in Create and Update Modes

You can use the following options to customize the actions of the create and the update main operation modes:

-e= CLASSNAME or —main-class= CLASSNAME Specifies the application entry point for standalone applications bundled into a modular or executable modular JAR file. -m= FILE or —manifest= FILE Includes the manifest information from the given manifest file. -M or —no-manifest Doesn’t create a manifest file for the entries. —module-version= VERSION Specifies the module version, when creating or updating a modular JAR file, or updating a non-modular JAR file. —hash-modules= PATTERN Computes and records the hashes of modules matched by the given pattern and that depend upon directly or indirectly on a modular JAR file being created or a non-modular JAR file being updated. -p or —module-path Specifies the location of module dependence for generating the hash. @ file Reads jar options and file names from a text file.

Operation Modifiers Valid Only in Create, Update, and Generate-index Modes

You can use the following options to customize the actions of the create ( -c or —create ) the update ( -u or —update ) and the generate-index ( -i or —generate-index= FILE) main operation modes:

-0 or —no-compress Stores without using ZIP compression.

Other Options

The following options are recognized by the jar command and not used with operation modes:

-h or —help [ :compat ] Displays the command-line help for the jar command or optionally the compatibility help. —help-extra Displays help on extra options. —version Prints the program version.

Examples of jar Command Syntax

% jar -tf foo.jar META-INF/ META-INF/MANIFEST.MF com/ com/foo/ com/foo/Hello.class com/foo/NameProvider.class META-INF/versions/10/com/ META-INF/versions/10/com/foo/ META-INF/versions/10/com/foo/NameProvider.class

As well as other information, the file META-INF/MANIFEST.MF , will contain the following lines to indicate that this is a multirelease JAR file with an entry point of com.foo.Hello .

. Main-Class: com.foo.Hello Multi-Release: true

Источник

Оцените статью