java 如何在java中使用命令行参数接受值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14510389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:29:33  来源:igfitidea点击:

how to accept values using command line argument in java

java

提问by Arnold Parge

Possible Duplicate:
What is “String args[]”? in Java

可能的重复:
什么是“String args[]”?在 Java 中

How do you accept values using command line argument in java? Whenever I try to do it, it shows

你如何在java中使用命令行参数接受值?每当我尝试这样做时,它都会显示

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

回答by Grambot

What have you tried? See this block for how to access the argsarray. You'll need to provide more details for why you're getting outofbounds exception, otherwise use this as a template.

你试过什么?有关如何访问args数组的信息,请参阅此块。您需要提供更多关于为什么会出现越界异常的详细信息,否则将其用作模板。

public static void main(String args[]) {
    for (int i = 0; i < args.length; i++) {
        System.out.println("Argument #" + i + " = " + args[i]);
    }
}

回答by Mechkov

from command line:

从命令行:

java MyApp firstParameter secondParameter

In your app;

在您的应用程序中;

public static void main(String[] argv) {

     for(String parameter: argv) {

        System.out.println(parameter + "\n")

     }

}