Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在eclipse中更正try-catch输出,但在cmd中不更正_Java_Eclipse_Error Handling_Cmd - Fatal编程技术网

Java 在eclipse中更正try-catch输出,但在cmd中不更正

Java 在eclipse中更正try-catch输出,但在cmd中不更正,java,eclipse,error-handling,cmd,Java,Eclipse,Error Handling,Cmd,我快为这件事发疯了。。 在调用我编写的Java程序时,我试图捕获从命令行输入的参数数量,在Eclipse中,我得到了正确的输出,但在win cmd中,我得到了ArrayIndexOutOfBoundException。 然后我使用了try-catch,同样,它在eclipse中工作,但在win-cmd中不工作……我缺少了什么 如果我插入所有的参数,它从两端都能完美地工作,那么当我想要得到缺少的参数时,它就不工作了 下面是我使用的代码的简化版本: public static PrintWriter

我快为这件事发疯了。。 在调用我编写的Java程序时,我试图捕获从命令行输入的参数数量,在Eclipse中,我得到了正确的输出,但在win cmd中,我得到了ArrayIndexOutOfBoundException。 然后我使用了try-catch,同样,它在eclipse中工作,但在win-cmd中不工作……我缺少了什么

如果我插入所有的参数,它从两端都能完美地工作,那么当我想要得到缺少的参数时,它就不工作了

下面是我使用的代码的简化版本:

public static PrintWriter outWrite;
public static String[] allInput;

    public static void main(String [] args) {

           // I first tried this

                if(args.length==0){
                    System.out.println("Missing arguments. Please try again");
                    System.exit(0);
                }
                if (args.length==1){
                    System.out.println("Missing second argument. Please try again");
                    System.exit(0);
                }


           // and then the try-catch, I leave both of them here so you can see them 

            try{
                myMethod(args[0]);
                String input2 = args[1];
            // here I'm just saving the output of myMethod to the input2
                try {
                    outWrite = new PrintWriter(input2, "UTF-8");
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } catch (ArrayIndexOutOfBoundsException e){
                System.out.println("Missing arguments. Please try again");
                System.exit(0);
            }
       }
我也看到了这一点,但没有成功

--编辑添加使用的命令行和精确错误:-- win中使用的命令行是java驱动程序,它应该给我在代码中编写的消息 相反,它给了我以下信息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
        at myPackage.Driver.main(Driver.java:31)
错误中的第31行对应于上面代码段中main方法内的第一行

在eclipse中,Driver.java:31有另一个数字,它正确地指向myMethodargs[0]行

正确的命令行是java myPackage.Driver fileIn.txt fileOut.txt,它在win cmd和eclipse中都能正常工作

任何帮助都会很棒!
谢谢:

好的,经过两天的尝试和头痛,我找到了解决办法:

该错误是由于Eclipse编译.java文件,而您需要在win或Mac中使用javac来编译

我无法在win中使用javac,因为它给了我javac不可识别的错误。我刚更换了PC,在这里查找帖子后,我能够使用以下方法编译它: javac*.java在我的src文件夹中

从那里,我终于能够从cmd和eclipse双方获得正确的输出,并提供正确的错误处理消息


我承认,我不能完全确定为什么只要我同时使用这两个参数,我就可以从cmd获得正确的输出,但上面的解决方案对我有效。

Wat是您在Windows cmd上用来调用应用程序的命令吗?您在Eclipse中设置的命令行参数是什么?查看抛出的异常的堆栈跟踪也很有帮助。@Hoopje-感谢您的回复,我通过添加命令行和错误编辑了本文,希望这对您有所帮助!