Documentation

The Java™ Tutorials
Hide TOC
Command-Line Arguments命令行参数
Trail: Essential Java Classes
Lesson: The Platform Environment
Section: Configuration Utilities

Command-Line Arguments命令行参数

A Java application can accept any number of arguments from the command line. Java应用程序可以从命令行接受任意数量的参数。This allows the user to specify configuration information when the application is launched.这允许用户在启动应用程序时指定配置信息。

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. 用户在调用应用程序时输入命令行参数,并在要运行的类的名称后指定它们。For example, suppose a Java application called Sort sorts lines in a file. 例如,假设一个名为Sort的Java应用程序对文件中的行进行排序。To sort the data in a file named friends.txt, a user would enter:要对名为friends.txt的文件中的数据进行排序,用户需要输入:

java Sort friends.txt

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. 启动应用程序时,运行时系统通过String数组将命令行参数传递给应用程序的主方法。In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".在上一个示例中,在一个数组中传递给Sort应用程序的命令行参数包含一个String"friends.txt"

Echoing Command-Line Arguments回显命令行参数

The Echo example displays each of its command-line arguments on a line by itself:Echo示例在一行上单独显示其每个命令行参数:

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}

The following example shows how a user might run Echo. 下面的示例显示了用户如何运行EchoUser input is in italics.用户输入为斜体。

java Echo Drink Hot Java
Drink
Hot
Java

Note that the application displays each word — Drink, Hot, and Java — on a line by itself. 请注意,应用程序显示每个单词—DrinkHotJava—在自己的行上。This is because the space character separates command-line arguments. 这是因为空格字符分隔命令行参数。To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks.要将DrinkHotJava解释为单个参数,用户可以通过将它们括在引号中来加入它们。

java Echo "Drink Hot Java"
Drink Hot Java

Parsing Numeric Command-Line Arguments解析数字命令行参数

If an application needs to support a numeric command-line argument, it must convert a String argument that represents a number, such as "34", to a numeric value. 如果应用程序需要支持数字命令行参数,则必须将表示数字(如“34”)的String参数转换为数字值。Here is a code snippet that converts a command-line argument to an int:以下是将命令行参数转换为int的代码段:

int firstArg;
if (args.length > 0) {
    try {
        firstArg = Integer.parseInt(args[0]);
    } catch (NumberFormatException e) {
        System.err.println("Argument" + args[0] + " must be an integer.");
        System.exit(1);
    }
}

parseInt throws a NumberFormatException if the format of args[0] isn't valid. 如果args[0]的格式无效,则parseInt引发NumberFormatExceptionAll of the Number classes — Integer, Float, Double, and so on — have parseXXX methods that convert a String representing a number to an object of their type.所有Number类—IntegerFloatDouble等—使用parseXXX方法将表示数字的String转换为其类型的对象。


Previous page: Properties
Next page: Environment Variables