java 接收命令行参数_java中的命令行参数_Java中的命令行参数

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

$

Note: If you are using

注意 :如果使用的是

Java 11 or higher, you don't need to compile the java source file explicitly. The java command will compile and run the class simultaneously.

Java 11或更高版本,则无需显式编译Java源文件。 java命令将同时编译并运行该类。

如何在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...".

在类编辑器中,右键单击并选择“运行方式”->“运行配置...”。

Eclipse Run Configurations

Eclipse运行配置

步骤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.

在弹出窗口中,单击“参数”选项卡。 然后在“程序参数”文本框中提供命令行参数值。

Eclipse Command Line Arguments

Eclipse命令行参数

步骤3:点击“运行”按钮 (Step 3: Click on the Run button)

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.

当您单击“运行”按钮时,将保存运行配置,并且程序将使用指定的命令行参数执行。

Eclipse Command Line Arguments Example

Eclipse命令行参数示例

If you run the class again, the saved run configuration will be used. So if you want to override the command-line arguments or remove them, you will have to open the run configurations window and make necessary changes.

如果再次运行该类,将使用保存的运行配置。 因此,如果要覆盖或删除命令行参数,则必须打开运行配置窗口并进行必要的更改。

结论 (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中的命令行参数

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值