java中的命令行参数
Command-line arguments in Java are used to pass arguments to the main program. If you look at the Java main method syntax, it accepts String array as an argument.
Java中的命令行参数用于将参数传递给主程序。 如果您查看Java main方法的语法,它将接受String数组作为参数。
When we pass command-line arguments, they are treated as strings and passed to the main function in the string array argument. The arguments have to be passed as space-separated values.
当我们传递命令行参数时,它们被视为字符串并传递给字符串数组参数中的main函数。 参数必须以空格分隔的值传递。
We can pass strings and primitive data types as command-line arguments. The arguments will be converted to strings and passed into the main method string array argument.
我们可以将字符串和原始数据类型作为命令行参数传递。 参数将转换为字符串,并传递给主方法字符串数组参数。
Java中的命令行参数 (Command Line Arguments in Java)
Let’s say we have a simple java class to print command line arguments values.
假设我们有一个简单的java类来打印命令行参数值。
package com.journaldev.examples;
public class CommandLineArguments {
public static void main(String[] args) {
System.out.println("Number of Command Line Argument = "+args.length);
for(int i = 0; i< args.length; i++) {
System.out.println(String.format("Command Line Argument %d is %s", i, args[i]));
}
}
}
If we run this class without any arguments, the output will be as follows.
如果我们在不带任何参数的情况下运行此类,则输出将如下所示。
$ java com/journaldev/examples/CommandLineArguments.java
Number of Command Line Argument = 0
Now, let's pass some arguments to the main class. We have to pass the arguments as space-separated values.
现在,让我们将一些参数传递给主类。 我们必须将参数作为以空格分隔的值进行传递。
$ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C"
Number of Command Line Argument = 3
Command Line Argument 0 is A
Command Line Argument 1 is B
Command Line Argument 2 is C
$ java com/journaldev/examples/CommandLineArguments.java 1 2 3
Number of Command Line Argument = 3
Command Line Argument 0 is 1
Command Line Argument 1 is 2
Command Line Argument 2 is 3
$
如何在Eclipse中传递命令行参数 (How to Pass Command Line Arguments in Eclipse)
We can also pass command-line arguments to a program in Eclipse using Run Configurations.
我们还可以使用“运行配置”将命令行参数传递给Eclipse中的程序。
步骤1:打开“类运行配置”设置 (Step 1: Open the Class Run Configurations Settings)
From the class editor, right click and chose "Run As" -> "Run Configurations...".
在类编辑器中,右键单击并选择“运行方式”->“运行配置...”。
步骤2:在“参数”选项卡中指定程序参数 (Step 2: Specify the Program Arguments in the Arguments Tab)
In the pop up window, click on the Arguments tab. Then provide the command line arguments value in the "Program Arguments" text box.
在弹出窗口中,单击“参数”选项卡。 然后在“程序参数”文本框中提供命令行参数值。
步骤3:点击“运行”按钮 ()
When you will click on the Run button, the run configurations will be saved and the program will execute with the specified command-line arguments.
当您单击“运行”按钮时,将保存运行配置,并且程序将使用指定的命令行参数执行。
结论 (Conclusion)
The command-line arguments are used to provide values that are essential to run the program. For example, we can specify the database credentials to be used by the program. We can specify the configuration file location from where the program should pick the required values.
命令行参数用于提供运行程序必不可少的值。 例如,我们可以指定程序要使用的数据库凭据。 我们可以指定程序应从中选择所需值的位置的配置文件位置。
Reference: Command-Line Arguments Oracle Docs
参考: 命令行参数Oracle Docs
翻译自: https://www.journaldev.com/32004/command-line-arguments-in-java
java中的命令行参数