1

I have a main method that creates an instance of a logic class

 public static void main(String[] args) {
 try {
 Logic logic = new Logic(args[0]);
 ....... do some stuff here
 } catch (Exception e) {
 System.out.println("Error Encountered Details: " + e);
 }
 }

the thing is that the programme requires a csv file to run, i have put it in the same directory as the .jar file but when i run from the command line i just get java.lang.arrayindexoutofbounds(0) error

what am i doing wrong

thanks

Georg Fritzsche
99.4k26 gold badges198 silver badges238 bronze badges
asked Dec 21, 2009 at 14:03
1
  • If your program requires an external file you'll need to be at least trying to access that somewhere in your code. If you posted that people would be better able to help you. Commented Dec 21, 2009 at 14:07

6 Answers 6

7

Your command line should look like:

java -cp {name_of_jar} {name_of_class} {name_of_csv}

It looks like you're not supplying the .csv file name (which will go in args[0]) ?

The above assumes that the main class is not defined in your .jar manifest. if it is, then use:

java -jar {name_of_jar} {name_of_csv}

It's a good practise to check the args[] array for the required info, btw, and generate a message such as "Missing .csv file name" or similar to avoid a nasty exception/stack trace.

answered Dec 21, 2009 at 14:06
Sign up to request clarification or add additional context in comments.

Comments

1

You need to pass an argument to your program. new Logic(argv[0]) indicates that the program expects at least one command line argument, like so:

java -jar ... somearg
answered Dec 21, 2009 at 14:06

Comments

1

You need to call your application with a command line something like:

java -jar MyAppJar myFile.csv

. You got that error message because you weren't supplying the file name.

answered Dec 21, 2009 at 14:07

Comments

1

args[0] will not contain anything unless you put it on the command line and you'll get an index out of bounds error. Your command should look like this...

java -jar myjar.jar c:\myfile.csv

args[0] will then contain c:\myfile.csv and you don't need to worry about its location.

answered Dec 21, 2009 at 14:07

Comments

1

You're not checking if the args have any items. Always check if args has contents before you try to access it. . . Others have answered how to pass the params in...

answered Dec 21, 2009 at 14:07

Comments

1

You need to invoke your program as something like

$ java -jar yourApp.jar csvFileName.csv

or, if the application isn't JAR-packaged,

$ java com.yourco.YourMainClass csvFileName.csv

The reason why you're getting the ArrayIndexOutOfBoundsException, is because the args array passed into the main method is of size zero (so doesn't have a first element). This happens if you don't pass any parameters to the program.

Incidentally you should typically guard against this by checking the number of arguments passed in and printing a more coherent error message, such as:

public static void main(String[] args)
{
 // Check mandatory argument(s) passed in
 if (args.length < 1)
 {
 System.err.println("Must supply filename of CSV input as first argument!");
 System.exit(1);
 }
 // rest of method, now you know you args[0] exists...
}
answered Dec 21, 2009 at 14:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.