Java 8 JAR 文件未在命令提示符下正确执行
Java 8 JAR file not executing correctly in command prompt
我有一个简单的 Java 程序,它从 IntelliJ IDEA 获取两个整数程序参数,然后将两个参数的总和打印到控制台。当我 运行 使用 IntelliJ 的程序时,它执行得很好。我为我的程序创建了一个名为 FirstProject_jar3
的 JAR 文件(这是我第二次尝试在命令提示符下将其设置为 运行)并尝试在命令提示符下使用 -classpath FirstProject.jar org.firstjavaproject.tutorial.TwoSum
执行它,但我收到一条错误消息 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at org.firstjavaproject.tutorial.TwoSum.main(TwoSum.java:5)
,我知道这意味着我的程序参数未配置。但是,我确实有两个使用 IDE 配置的整数参数(3 和 5)。任何想法为什么它不起作用?请参阅下面的代码、文件路径、命令提示符屏幕和 IDE 配置屏幕。如果您需要更多详细信息来帮助我,请告诉我。
TwoSum.java
package org.firstjavaproject.tutorial; // imports package
public class TwoSum { // start class "TwoSum"
public static void main(String[] args) { //define the method
int a = Integer.parseInt(args[0]); //define variable "a" with the first integer in the program arguments
int b = Integer.parseInt(args[1]); //define variable "b" with the first integer in the program arguments
int sum = a + b; //add variable "a" and "b"
System.out.println ("a + b = " + sum); //print out the sum of the integers in a statement
}
}
output from IDE
a + b = 8
Process finished with exit code 0
file paths in IntelliJ
cmd screen
IntelliJ configuration screen for TwoSum
更新答案:
感谢提供答案的人!现在我需要将数字输入命令提示符才能使我的代码正常工作。
如果你在命令提示符下运行,你没有在Idea中配置参数。尝试
java -classpath FirstProject.jar org.firstjavaproject.tutorial.TwoSum 3 5
命令末尾的两个值。
为了通过您的 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
并且控制台 Window 中的输出应该如下所示:
结果是: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
我有一个简单的 Java 程序,它从 IntelliJ IDEA 获取两个整数程序参数,然后将两个参数的总和打印到控制台。当我 运行 使用 IntelliJ 的程序时,它执行得很好。我为我的程序创建了一个名为 FirstProject_jar3
的 JAR 文件(这是我第二次尝试在命令提示符下将其设置为 运行)并尝试在命令提示符下使用 -classpath FirstProject.jar org.firstjavaproject.tutorial.TwoSum
执行它,但我收到一条错误消息 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at org.firstjavaproject.tutorial.TwoSum.main(TwoSum.java:5)
,我知道这意味着我的程序参数未配置。但是,我确实有两个使用 IDE 配置的整数参数(3 和 5)。任何想法为什么它不起作用?请参阅下面的代码、文件路径、命令提示符屏幕和 IDE 配置屏幕。如果您需要更多详细信息来帮助我,请告诉我。
TwoSum.java
package org.firstjavaproject.tutorial; // imports package
public class TwoSum { // start class "TwoSum"
public static void main(String[] args) { //define the method
int a = Integer.parseInt(args[0]); //define variable "a" with the first integer in the program arguments
int b = Integer.parseInt(args[1]); //define variable "b" with the first integer in the program arguments
int sum = a + b; //add variable "a" and "b"
System.out.println ("a + b = " + sum); //print out the sum of the integers in a statement
}
}
output from IDE
a + b = 8
Process finished with exit code 0
file paths in IntelliJ
cmd screen
IntelliJ configuration screen for TwoSum
更新答案: 感谢提供答案的人!现在我需要将数字输入命令提示符才能使我的代码正常工作。
如果你在命令提示符下运行,你没有在Idea中配置参数。尝试
java -classpath FirstProject.jar org.firstjavaproject.tutorial.TwoSum 3 5
命令末尾的两个值。
为了通过您的 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
并且控制台 Window 中的输出应该如下所示: 结果是: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