Run a Java Application from the Command Line – 从命令行中运行一个Java应用程序

最后修改: 2019年 5月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Typically, every meaningful application includes one or more JAR files as dependencies. But there are times a JAR file itself represents a standalone application or a web application.

通常情况下,每个有意义的应用程序都包括一个或多个JAR文件作为依赖关系。但有些时候,一个JAR文件本身就代表了一个独立的应用程序或一个Web应用程序。

Here we’ll focus on the standalone application scenario. From now on, we’ll refer to it as a JAR application.

在这里,我们将集中讨论独立应用程序的情况。从现在开始,我们将把它称为一个JAR应用程序。

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.

在本教程中,我们将首先学习如何创建一个JAR应用程序。随后,我们将学习如何在有或没有命令行参数的情况下运行一个JAR应用程序

2. Create a JAR Application

2.创建一个JAR应用程序

JAR file can contain one or more main classes. Each main class is the entry point of an application. So, a JAR file can theoretically contain more than one application, but it has to contain at least one main class to be able to run.

一个JAR文件可以包含一个或多个主类。每个主类都是一个应用程序的入口。因此,理论上一个JAR文件可以包含一个以上的应用程序,但它必须至少包含一个主类才能运行。

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.

一个JAR文件可以在其manifest文件中设置一个入口点。在这种情况下,该JAR文件是一个executable JAR。主类必须包含在该JAR文件中。

First, let’s see a quick example of how to compile our classes and create an executable JAR with a manifest file:

首先,让我们看一个快速的例子,看看如何编译我们的类并创建一个带有清单文件的可执行JAR。

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

A nonexecutable 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.

不可执行的JAR只是一个在清单文件中没有定义Main-Class的JAR文件。正如我们稍后所见,我们仍然可以运行包含在JAR文件本身中的主类。

Here’s how we would create a nonexecutable JAR without a manifest file:

下面是我们如何创建一个没有清单文件的不可执行的JAR。

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

3. Java Command-Line Arguments

3 Java命令行参数

Just like any application, a JAR application accepts any number of arguments, including zero arguments. It all depends on the application’s need.

就像任何应用程序一样,一个JAR应用程序接受任何数量的参数,包括零参数。这一切都取决于应用程序的需要。

This allows the user to specify configuration information when the application is launched.

这允许用户在启动应用程序时指定配置信息。

As a result, the application can avoid hard-coded 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, @.

一个参数可以包含任何字母数字字符、unicode字符和可能是shell允许的一些特殊字符,例如,@。

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.

通常,对于一个典型的Java应用程序,在调用应用程序时,用户在类的名称后面输入命令行参数。

However, that’s not always the case for JAR applications.

然而,对于JAR应用程序来说,情况并非总是如此。

As we 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.

正如我们所讨论的,Java主类的入口是main方法参数都是Strings,并以String数组的形式传递给main方法。

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.

也就是说,在应用程序内部,我们可以将String数组的任何元素转换为其他数据类型,如charintdouble,它们的wrapper类或其他适当类型。

4. Run an Executable JAR with Arguments

4.运行一个可执行的JAR和参数

Let’s see the basic syntax for running an executable JAR file with arguments:

让我们看看带参数运行可执行JAR文件的基本语法。

java -jar jar-file-name [args …]

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.

先前创建的可执行JAR是一个简单的应用程序,只是打印出传入的参数。我们可以用任何数量的参数来运行它。

Here’s an example with two arguments:

下面是一个有两个参数的例子。

$ java -jar JarExample.jar "arg 1" arg2@

We’ll see this output in the console:

我们会在控制台看到这个输出。

Hello Baeldung Reader in JarExample!
There are 2 argument(s)!
Argument(1):arg 1
Argument(2):arg2@

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.

因此,当调用一个可执行的JAR时,我们不需要在命令行上指定主类的名字。我们只需在JAR文件名之后添加我们的参数。如果我们在可执行的JAR文件名之后提供一个类名,那么它就会成为实际的主类的第一个参数。

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.

大多数时候,一个JAR应用程序是一个可执行的JAR。一个可执行的JAR最多只能有一个主类在manifest文件中定义。

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 nonexecutable JAR. We’ll see exactly how in the next section.

因此,同一可执行JAR文件中的其他应用程序不能在清单文件中设置,但我们仍然可以从命令行中运行它们,就像对不可执行的JAR一样。我们将在下一节中看到具体方法。

5. Run a Nonexecutable JAR with Arguments

5.运行带有参数的不可执行的JAR

To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar.

要在一个不可执行的JAR文件中运行一个应用程序,我们必须使用-cp选项而不是-jar

We’ll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute:

我们将使用 -cp选项(classpath的简称)来指定包含我们要执行的类文件的JAR文件。

java -cp jar-file-name main-class-name [args …]

java -cp jar-file-name main-class-name [args …]

As we can see, in this case, we’ll have to include the main class name in the command line, followed by arguments.

正如我们所看到的,在这种情况下,我们必须在命令行中包括主类的名称,然后是参数。

The nonexecutable JAR created earlier contains the same simple application. We can run it with any (including zero) arguments.

先前创建的不可执行的JAR包含同样的简单应用程序。我们可以用任何(包括零)参数运行它。

Here’s an example with two arguments:

下面是一个有两个参数的例子。

$ java -cp JarExample2.jar com.baeldung.jarArguments.JarExample "arg 1" arg2@

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

而且,就像我们上面看到的那样,我们会看到这样的输出。

Hello Baeldung Reader in JarExample!
There are 2 argument(s)!
Argument(1):arg 1
Argument(2):arg2@

6. Conclusion

6.结语

In this article, we learned two ways of running a JAR application on the command line with or without arguments.

在这篇文章中,我们学习了在命令行上运行JAR应用程序的两种方法,即带或不带参数。

We also demonstrated that an argument could contain spaces and special characters (when allowed by the shell).

我们还演示了一个参数可以包含空格和特殊字符(当shell允许时)。

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

像往常一样,这些例子的代码可以在GitHub上找到