The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.tx
t的文件中的数据进行排序,用户需要输入:
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 启动应用程序时,运行时系统通过String
s. 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"
。
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
. Echo
。User 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. Drink
、Hot
和Java
在自己的行上。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.Drink
、Hot
和Java
解释为单个参数,用户可以通过将它们括在引号中来加入它们。
java Echo "Drink Hot Java" Drink Hot Java
If an application needs to support a numeric command-line argument, it must convert a 如果应用程序需要支持数字命令行参数,则必须将表示数字(如“34”)的String
argument that represents a number, such as "34", to a numeric value. 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
引发NumberFormatException
。All 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
类Integer
、Float
、Double
等使用parseXXX
方法将表示数字的String
转换为其类型的对象。