0

I'm driving myself a lil crazy for this.. I am trying to catch the number of arguments inputed from command line when calling the Java program I wrote, in Eclipse I get the correct output but in win cmd I get the ArrayIndexOutOfBoundException. I then used try-catch, again, it works in eclipse but not in win cmd...what am I missing?

if I insert all the arguments it works perfectly from both ends, it's just when I want to get the missing arguments that it doesn't work.

below a simplified version of the code I used:

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);
            }
       }

I looked at this too Eclipse gives different output than cmd using runtime exec without success

---EDIT adding command line used and precise error: ---- command line used in win is java Driver which should give me message I wrote in the code instead it gives me the following:

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

line 31 in the error corresponds to the first line inside the main() method in the snippet above.

in eclipse the (Driver.java:31) has another number at is correctly points to the myMethod(args[0]) line

the correct command line is java myPackage.Driver fileIn.txt fileOut.txt which works perfectly from both win cmd and eclipse

any help would be awesome! thanks :)

TriRook
  • 147
  • 1
  • 3
  • 18
  • Wat is the command you used on the Windows cmd to call your application? And what are the command-line arguments you set in Eclipse? It is also helpful to see the stack trace of the exception which is thrown. – Hoopje Dec 03 '14 at 17:39
  • @Hoopje - thanks for the reply, I edited the main post by adding command line and error, hope this helps! – TriRook Dec 03 '14 at 17:52

1 Answers1

0

alright, after two days of attempts and headache I found a solution:

the error was due to the fact that Eclipse compiles the .java files while you need to do it with javac in win or Mac.

I could not use javac in win since it was giving me the javac is not recognized error (I just changed PC) and after looking up the post here javac is not recognized as an internal or external command, operable program or batch file, I was able to compile it using: javac *.java in my src folder

from there I was finally able to get the correct output from both sides (cmd and eclipse) with the correct error handling messages

I admit I am not totally sure about why I was getting the correct output from cmd as long as I used both arguments, but the solution above worked for me.

Community
  • 1
  • 1
TriRook
  • 147
  • 1
  • 3
  • 18