为了通过您的 IDE 运行此代码,IDE 中将有一个特定位置,您可以在其中应用命令行参数进行测试。这些测试参数显然对于您的代码运行是绝对必要的,以免产生异常。你显然知道这一点。
然而,在远离 IDE 的现实世界中,当您的应用程序被编译时,您作为命令行参数应用的那些值不再是事物整体图景的一部分……毕竟,它们只是用于测试。现在这些值需要作为 real 命令行参数提供,正如其他人在这里很好地指出的那样。
java -jar "C:\Path_To_My_JAR_File\FirstProject.jar" 3 5
与您的情况无关,但您是否注意到路径是如何用引号 ("...") 括起来的。这是为了防止路径中包含一个或多个空格。任何包含一个或多个空格的单个命令行参数都应封装在引号内,否则该参数将被解释为一个或多个参数,因为命令行参数实际上由...空格分隔。
获取命令行参数是一回事,但确保这些参数真正有效是另一回事。一般来说,推出一个异常是好的,并且足够解释,但有时可能需要接管并输出更多或完全防止异常。验证提供的参数可为您提供控制。举个简单的例子:
public static void main(String[] args) { //define the method
/* Here, sum is declared as double type because this code can
accept ay number of both integer and floating point values
to sum up. */
double sum = 0.0d;
/* This StringBuilder object is used to build the equation that
will be displayed within the Console Window. It will contain
'only' supplied valid arguments and only if there is more than
one argument. */
StringBuilder equation = new StringBuilder("");
// What Command Line Arguments were suppied...
switch (args.length) {
// If no arguments were suppied!
case 0:
System.out.println("Result is 0 since no arguments were supplied to sum up.");
break;
case 1:
// If only one argument was supplied!
if (args[0].matches("-?\\d+(\\.\\d+)?")) {
sum += Double.parseDouble(args[0]);
}
System.out.println("Result is " + sum
+ " since only one argument was supplied to sum up.");
break;
default:
// If more than two arguments were supplied.
/* The code below allows for two or more Command Line Argument
values to be supplied and processed accordingly. */
for (String value : args) {
/* Validate that the supplied argument is indeed an integer
or floating point value either signed or unsigned. */
if (value.matches("-?\\d+(\\.\\d+)?")) {
// It's valid so parse it as Double type...
sum += Double.parseDouble(value);
// Build upon the equation for display...
// --------------------------------------
if (!equation.toString().isEmpty()) {
equation.append(" + ");
}
equation.append(value);
// --------------------------------------
}
else {
// Argument is found to be invalid so skip it. Inform User.
System.out.println("Skipping the supplied argument (" + value
+ ") since it is not a valid numerical value!");
}
}
System.out.println("Result is: " + sum);
}
/* Print out the equation that generated the
sum of the values supplied but only if more
than one argument was supplied. */
if (args.length > 1) {
System.out.println ("Equation:");
System.out.println (equation +
(equation.toString().isEmpty() ? "" : " = ")
+ sum);
}
}
编译成.jar 文件后,您可以像这样启动它:
java -jar "C:\Path_To_My_JAR_File\FirstProject.jar" 3 5 122 12.67 8.8 11
控制台窗口中的输出应如下所示:
结果是:162.47
方程:
3 + 5 + 122 + 12.67 + 8.8 + 11 = 162.47
如果没有提供参数:
java -jar "C:\Path_To_My_JAR_File\FirstProject.jar"
输出将是:
Result is 0 since no arguments were supplied to sum up.
而且,如果只提供一个参数:
java -jar "C:\Path_To_My_JAR_File\FirstProject.jar" 8.13
输出将是:
Result is 8.13 since only one argument was supplied to sum up.
如果在参数列表中提供了一个或多个无效参数:
java -jar "C:\Path_To_My_JAR_File\FirstProject.jar" 3 5r 122 12.67 8.u8 11
输出将是:
Skipping the supplied argument (5r) since it is not a valid numerical value!
Skipping the supplied argument (8.u8) since it is not a valid numerical value!
Result is: 148.67
Equation:
3 + 122 + 12.67 + 11 = 148.67