Java 中的命令行參數是什麼?
如何訪問java命令行參數?
在 java 中訪問命令行參數的方法非常簡單。在我們的 java 代碼中使用這些參數很簡單。它們存儲為傳遞給main()的字符串數組。它主要被命名為args。看看下面代碼片段中的公共標頭。
public static void main(String[] args){…}
例子
讓我們看一個下面詳細解釋的例子。
// Program to check for command line arguments
public class Example {
public static void main(String[] args) {
// check if the length of args array is < 0
if (args.length <= 0) {
System.out.println("No command line arguments found.");
} else {
System.out.println("The first command line argument is: " + args[0]);
System.out.println("All of the command line arguments are: ");
// iterating the args array and printing all of the command line arguments
for (String index : args)
System.out.println(index);
}
}
}
執行
要執行該程序,請按以下方式在命令行上傳遞參數。我們在這裡使用 IntelliJ IDE,您可以使用任何您的選擇。對於 IntelliJ,選擇選項“運行”→“編輯配置”。

我的名字是安德魯。
輸出
第一個命令行參數是:My 所有命令行參數是:My name is Andrew。
GO TO FULL VERSION