Java 简明教程
Java - Command Line Arguments
在 Java 中, command line arguments 是在应用执行期间将输入传递到 Java 程序的一种方法。可以有多种方式将 Command line arguments 传递到 Java 应用或程序。最常见的是,在执行 java program 的控制台传入命令行参数。在程序执行期间提供的命令行参数在 main() 方法中捕获为 string 数组。
In Java, command line arguments is a way to pass inputs to the java program during application execution. Command line arguments can be passed by multiple ways to Java application or program. Most commonly, command line arguments are passed from console where a java program is executed. command-line arguments, provided during program execution, are captured in the main() method as a string array.
Passing & Accessing Command Line Arguments
考虑以下传递命令行参数的语法:
Consider the below syntax of passing command-line arguments:
javac tester.java
java tester arg1 arg2 arg3
在此我们编译了一个 Java 文件 tester.java,并在使用 Java 运行测试器类时,我们传递了三个由空格分隔的参数。我们可以将任意数量的命令行参数传递到 Java 程序。 Java Virtual Machine (JVM) 将这些输入封装到 args[] 数组中。我们可以使用 args.length 检查传入的参数数量。如果没有命令行参数,则此数组为空。
Here we’ve compiled one java file namely tester.java and while running the tester class using java, we’re passing three arguments which are separated by space. We can pass any number of command line arguments to java program. The Java Virtual Machine (JVM) encapsulates these inputs into the args[] array. We can check the number of arguments passed using args.length. In case no command line argument is present, then this array is empty.
class Tester {
public static void main(String[] args){
...
}
}
我们可以使用命令行参数来传递字符串、整数和任何其他原始值。传入的每个参数都会按照输入的顺序保存在数组中,从 args[0] 开始。
We can pass Strings, integers and any other primitive value using command line arguments. Each argument passed is available in the array in the order of entry, starting from args[0].
Benefits of Command Line arguments
-
Command line arguments allows to configure the application behavior by passing the arguments before start of the application. Batch processes are one of the example where command line arguments are heavily used to execute java commands with configuration parameters. This mechanism enables dynamic parameterization of Java programs through console inputs, enhancing versatility and interactivity.
-
Command line arguments facilites user input retrieval and manipulation in case of console based applications
Example of Single Command Line Argument
在此示例中,我们正在检查是否仅传入了一个参数来表示 name。如果未传入任何参数或传入了一个以上参数,我们打印错误消息,表示传入参数数无效。否则,我们打印带称呼的 name。
In this example, we’re checking if exactly one argument is passed to represent name. In case, no arguments are passed or more than one argument is passed, we print the error message as invalid number of arguments passed. Otherwise, we’re printing the name with a salutation.
public class Tester {
// args array represents the command line arguments passed
public static void main(String[] args) {
// if only one argument is passed
if(args.length == 1) {
String name = args[0];
System.out.println("Welcome " + name + "!");
}else { // otherwise print an error message
System.out.println("Invalid Command line argument(s) passed.");
}
}
}
Output
让我们编译并运行上述程序,没有任何命令行参数,这将产生以下结果:
Let us compile and run the above program without any command line argument, this will produce the following result −
D:\test>javac Tester.java
D:\test>java Tester
Invalid Command line argument(s) passed.
在这里,我们使用 javac 命令编译了 java 代码,然后在不带任何命令行参数的情况下使用 java 命令运行。让我们再次使用所需的参数运行 java 命令。
Here, we’ve compiled the java code using javac command and then run using java command without any command line argument. Let’s run the java command again with required parameter.
D:\test>java Tester Mahesh
Welcome Mahesh!
Example of Multiple Command Line Arguments
在此示例中,我们正在检查是否仅传入两个参数来表示 name 和 age。年龄是个数,我们使用 parseInt() 方法来解析参数。如果未传入任何参数或传入两个以上参数,我们打印错误消息,表示传入参数数无效。否则,我们打印收到的 name 和 age。
In this example, we’re checking if exactly two arguments are passed to represent name and age. Age being a number, we’re parsing the argument using parseInt() method. In case, no arguments are passed or more than two arguments are passed, we print the error message as invalid number of arguments passed. Otherwise, we’re printing the name and age received.
public class Tester {
// args array represents the command line arguments passed
public static void main(String[] args) {
// if two arguments are passed
if(args.length == 2) {
String name = args[0];
// parse the age as int
int age = Integer.parseInt(args[1]);
System.out.println("Name: " + name + ", age: " + age);
}else { // otherwise print an error message
System.out.println("Invalid Command line argument(s) passed.");
}
}
}
Output
让我们编译并运行上述程序,没有任何命令行参数,这将产生以下结果:
Let us compile and run the above program without any command line argument, this will produce the following result −
D:\test>javac Tester.java
D:\test>java Tester
Invalid Command line argument(s) passed.
在此,我们使用 javac 命令编译了 Java 代码,然后使用 java 命令运行,且没有任何命令行参数。让我们再次使用必需的参数运行 Java 命令。
Here, we’ve compiled the java code using javac command and then run using java command without any command line argument. Let’s run the java command again with required parameters.
D:\test>java Tester Mahesh 40
Name: Mahesh, age: 40
Conclusion
Java 命令行参数对于创建参数化 Java 程序非常有用,这些程序可以动态接受参数。用户对程序的行为具有运行时控制,因为可以将参数传递给 main() 方法。使用命令行参数,我们可以在运行时管理程序的输出、设置设置参数以及指定输入文件,而没有任何编译时间依赖关系。
Java command-line arguments are very useful to create parameterized java programs which can accept the parameters dynamically. Users have runtime control over the behavior of the program as arguments can be passed to the main() method. With command line arguments, we can manage program’s output, set setup parameters, and specify input files at runtime without any compile time dependencies.
我们可以通过多种方法向一个 Java 程序传递命令行参数。各种 IDE 支持在其执行配置中提供参数。而且,它们易于使用。当你运行代码时,它可以设置在程序的配置文件中或者在命令行上输入。
We can pass command-line arguments in many ways to a java program. Various IDEs supports providing the arguments in their execution configurations. Also, they are easy to employ. When you run the code, it can be set in the program’s configuration file or supplied on the command line.