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 :)