【发布时间】:2015-12-29 13:31:44
【问题描述】:
我正在阅读“Java:初学者指南”,第六版,我遇到了一个地方,我正在准确地输入书中所说的内容,但我得到了不想要的输出。
这是我的课:
// Display all command-line information
public class CLDemo {
public static void main(String args[]) {
System.out.println("There are " + args.length + " command-line arguments");
System.out.println("They are: ");
for(int i = 0; i < args.length; i++)
System.out.println("arg[" + i + "]: " + args[i]);
}
}
我的控制台输出:
There are 0 command-line arguments
They are:
所需的控制台输出:
There are 3 command-line arguments
There are:
arg[0]: one
arg[1]: two
arg[2]: three
我正在为 Java 开发人员使用 Eclipse IDE
版本:Kepler Service Release 1 内部版本号:20130919-0819
任何想法关于:为什么我的代码中的参数数量与书中的参数数量不匹配将不胜感激。
更新:
我的问题的解决方案非常简单。我一直在 Eclipse 中通过按下“运行”按钮而不指定参数来运行示例项目,就像我在本书的前 164 页所做的那样,没有任何问题。这本书指示我从 命令行 执行程序,如下所示:
java CLDemo one two three // where one two three are the arguments passed
感谢那些引导我找到解决方案的人。
【问题讨论】:
-
你没有为程序提供任何参数
-
你输入什么命令来启动应用程序?如果你通过eclipse运行,你必须在你的运行配置中配置参数
-
我在 Eclipse 中按下运行按钮。我将尝试从命令行编译/运行该文件,看看它做了什么。
-
@AdrianB 在 Eclipse 中使用
run configuration传递命令行参数