Command-Line Arguments in Java – Java中的命令行参数

最后修改: 2019年 9月 6日

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

1. Introduction

1.绪论

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.

在这个简短的教程中,我们将探讨如何能在Java中处理命令行参数。

2. Accessing Command-Line Arguments in Java

2.在Java中访问命令行参数

Since the main method is the entry point of a Java application, the JVM passes the command-line arguments through its arguments.

由于main方法是一个Java应用程序的入口点,JVM通过其参数传递命令行参数。

The traditional way is to use a String array:

传统的方法是使用一个String数组。

public static void main(String[] args) {
    // handle arguments
}

However, Java 5 introduced varargs, which are arrays in sheep’s clothing. Therefore, we can define our main with a String vararg:

然而,Java 5引入了varargs,它是披着羊皮的数组。因此,我们可以用一个String vararg来定义我们的main

public static void main(String... args) {
    // handle arguments
}

They’re identical, therefore choosing between them is entirely up to personal taste and preference.

它们是相同的,因此在它们之间进行选择完全取决于个人的品味和偏好。

The method parameter of the main method contains the command-line arguments in the same order we passed at execution. If we want to access how much arguments did we get, we only have to check the length of the array.

main方法的参数包含命令行参数,其顺序与我们在执行时传递的参数相同。如果我们想获得多少参数,我们只需检查数组的length

For example, we can print the number of arguments and their value on the standard output:

例如,我们可以在标准输出上打印参数的数量和它们的值。

public static void main(String[] args) {
    System.out.println("Argument count: " + args.length);
    for (int i = 0; i < args.length; i++) {
        System.out.println("Argument " + i + ": " + args[i]);
    }
}

Note that in some languages, the first argument will be the name of the application. On the other hand, in Java, this array contains only the arguments.

注意,在某些语言中,第一个参数将是应用程序的名称。另一方面,在Java中,这个数组只包含参数。

3. How to Pass Command-Line Arguments

3.如何传递命令行参数

Now that we have an application that handles command-line arguments, we’re eager to try it. Let’s see what options we have.

现在我们有了一个可以处理命令行参数的应用程序,我们急于尝试它。让我们看看我们有哪些选项。

3.1. Command Line

3.1.命令行

The most obvious way is the command-line. Let’s assume we already compiled the class com.baeldung.commandlinearguments.CliExample with our main method in it.

最明显的方式是命令行。让我们假设我们已经编译了com.baeldung.commandlinearguments.CliExample类,其中有我们的main方法。

Then we can run it with the following command:

然后我们可以用以下命令运行它。

java com.baeldung.commandlinearguments.CliExample

It produces the following output:

它产生的输出结果如下。

Argument count: 0

Now, we can pass arguments after the class name:

现在,我们可以在类名后面传递参数。

java com.baeldung.commandlinearguments.CliExample Hello World!

And the output is:

而输出是。

Argument count: 2
Argument 0: Hello
Argument 1: World!

Usually, we publish our application as a jar file, not as a bunch of .class files. Let’s say, we packaged it in the cli-example.jar, and we set com.baeldung.commandlinearguments.CliExample as the main class.

通常,我们把我们的应用程序作为一个jar文件发布,而不是作为一堆.class文件。比方说,我们把它打包在cli-example.jar中,并把com.baeldung.commandlinearguments.CliExample作为主类。

Now we can run it without arguments the following way:

现在我们可以按以下方式运行它,不需要参数。

java -jar cli-example.jar

Or with arguments:

或者说有了争论。

java -jar cli-example.jar Hello World!
Argument count: 2 
Argument 0: Hello 
Argument 1: World!

Note, that Java will treat every argument we pass after the class name or the jar file name as the arguments of our application. Therefore, everything we pass before that are arguments for the JVM itself.

请注意,Java将把我们在类名或jar文件名之后传递的每个参数都视为我们应用程序的参数。因此,我们在这之前传递的所有东西都是JVM本身的参数。

3.2. Eclipse

日蚀

While we’re working on our application, we’ll want to check if it works the way we want.

当我们在处理我们的应用程序时,我们要检查它是否按照我们想要的方式工作。

In Eclipse, we can run applications with the help of run configurations. For example, a run configuration defines which JVM to use, what is the entry point, the classpath, and so on. And of course, we can specify command-line arguments.

在Eclipse中,我们可以在运行配置的帮助下运行应用程序。例如,运行配置定义了要使用哪个JVM,什么是入口点,classpath等等。当然,我们还可以指定命令行参数。

The easiest way to create an appropriate run configuration is to right-click on our main method, then choose Run As > Java Application from the context menu:

创建一个适当的运行配置的最简单方法是右击我们的main方法,然后从上下文菜单中选择Run As > Java Application

eclipse run

With this, we instantly run our application with settings that honor our project settings.

有了这个,我们就可以立即以尊重我们项目设置的设置来运行我们的应用程序。

To provide arguments, we should then edit that run configuration. We can do it through the Run > Run Configurations… menu option. Here, we should click the Arguments tab and fill the Program arguments textbox:

为了提供参数,我们应该编辑该运行配置。我们可以通过Run > Run Configurations…菜单选项来完成。在这里,我们应该点击Arguments标签,并填写Program arguments文本框。

eclipse configure

Hitting Run will run the application and pass the arguments we just entered.

点击Run将运行应用程序并传递我们刚刚输入的参数。

3.3. IntelliJ

3.3.IntelliJ

IntelliJ uses a similar process to run applications. It calls these options simply as configurations.

IntelliJ使用一个类似的过程来运行应用程序。它把这些选项简单地称为配置。

First, we need to right-click on the main method, then choose Run ‘CliExample.main()’:

首先,我们需要右键单击main方法,然后选择运行’CliExample.main()’:

intellij run

This will run our program, but it will also add it to the Run list for further configuration.

这将运行我们的程序,但它也将把它添加到Run列表中,以便进一步配置。

So, then to configure arguments, we should choose Run > Edit Configurations… and edit the Program arguments textbox:

所以,然后要配置参数,我们应该选择Run > Edit Configurations…并编辑Program arguments文本框。

intellij configure

After that, we should hit OK and rerun our application, for example with the run button in the toolbar.

之后,我们应该点击确定并重新运行我们的应用程序,例如用工具栏上的运行按钮。

3.4. NetBeans

3.4 NetBeans

NetBeans also falls into line with its running and configuration processes.

NetBeans也与它的运行和配置过程保持一致。

We should run our application first by right-clicking on the main method and choosing Run File:

我们应该首先运行我们的应用程序,右键单击main方法,选择运行文件:

netbeans run

Like before, this creates a run configuration and runs the program.

像以前一样,这将创建一个运行配置并运行程序。

Next, we have to configure the arguments in that run configuration. We can do that by choosing Run > Set Project Configuration > Customize… Then we should Run on the left and fill the Arguments text field:

接下来,我们必须在该运行配置中配置参数。我们可以通过选择Run > Set Project Configuration > Customize… 然后我们应该在左边Run,并填写Arguments文本字段。

netbeans configure

After that, we should hit OK and start the application.

之后,我们应该点击确定并启动应用程序。

4. Third-Party Libraries

4.第三方图书馆

Manual handling of the command-line arguments is straightforward in simple scenarios. However, as our requirements become more and more complex, so does our code. Therefore, if we want to create an application with multiple command-line options, it would be easier to use a third-party library.

在简单的情况下,手动处理命令行参数是很直接的。然而,随着我们的需求变得越来越复杂,我们的代码也越来越复杂。因此,如果我们想创建一个具有多个命令行选项的应用程序,使用第三方库会更容易。

Fortunately, there’re a plethora of those libraries which support most use cases. Two popular examples are Picocli and Spring Shell.

幸运的是,有大量的这些库可以支持大多数使用情况。两个流行的例子是PicocliSpring Shell

5. Conclusion

5.总结

It’s always a good idea to make your application’s behavior configurable. In this article, we saw how to do that using command-line arguments. Additionally, we covered various ways to pass those arguments.

让你的应用程序的行为可配置,总是一个好主意。在这篇文章中,我们看到了如何使用命令行参数来做到这一点。此外,我们还介绍了传递这些参数的各种方法。

As usual, the examples are available over on GitHub.

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