【问题标题】:Java 8 JAR file not executing correctly in command promptJava 8 JAR 文件在命令提示符下未正确执行
【发布时间】:2021-11-13 05:12:35
【问题描述】:

我有一个简单的 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

更新答案: 感谢那些提供答案的人!现在我需要在命令提示符中输入数字才能使我的代码正常工作。

【问题讨论】:

    标签: java intellij-idea cmd arguments


    【解决方案1】:

    如果您在命令提示符下运行,您没有在 Idea 中配置参数。试试

    java -classpath FirstProject.jar org.firstjavaproject.tutorial.TwoSum 3 5
    

    在命令末尾有两个值。

    【讨论】:

      【解决方案2】:

      为了通过您的 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
      

      【讨论】:

      • 这比我希望的答案有点长,但谢谢你的例子。
      猜你喜欢
      • 2019-11-07
      • 2011-08-12
      • 2015-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2019-12-20
      相关资源
      最近更新 更多