2014-12-03 50 views
0

我開着自己律瘋了這個.. 我追趕的調用Java程序,我寫的時候,在Eclipse命令行inputed參數的個數我得到正確的輸出,但在win cmd中,我得到了ArrayIndexOutOfBoundException。 然後我用的try-catch,再次,它工作在食,但不是在贏CMD ......我失去了什麼?正確的try-catch輸出食,但不是在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); 
      } 
     } 

我搜索這太Eclipse gives different output than cmd using runtime exec沒有成功

--- EDIT添加命令行中使用和精確的誤差:---- 在獲勝的命令行是java Driver這應該給我留言我在寫代碼,而不是 它給了我下面的:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
     at myPackage.Driver.main(Driver.java:31) 

line 31在誤差對應於在上面的代碼段中的main()方法內的第一行。

在Eclipse中(Driver.java:31)有另一個號碼的正確指向myMethod(args[0])

正確的命令行是java myPackage.Driver fileIn.txt fileOut.txt從雙贏CMD和eclipse

任何幫助將是真棒完美的作品! 謝謝:)

+0

笏是你的Windows CMD用來調用你的應用程序的命令?你在Eclipse中設置的命令行參數是什麼?查看拋出異常的堆棧跟蹤也很有幫助。 – Hoopje 2014-12-03 17:39:19

+0

@Hoopje - 感謝您的答覆,我通過添加命令行和錯誤編輯的主貼,希望這有助於! – TriRook 2014-12-03 17:52:41

回答

0

好了,嘗試之後,頭痛兩天我發現了一個解決方案:

錯誤是由於了Eclipse編譯.java文件,而你需要贏與javac做到這一點的事實或Mac。

我不能取勝使用javac,因爲這是給我的javac is not recognized error(我只是改變了PC),並在這裏尋找這一職位javac is not recognized as an internal or external command, operable program or batch file後,我能夠使用到編譯: javac *.java在我src文件夾

從那裏我終於能夠得到正確的輸出從雙方(cmd和日食)與正確的錯誤處理消息

我承認我不完全確定爲什麼我得到正確的輸出從cmd只要我使用了兩個參數,但上面的解決方案適用於我。