1

Right, so I managed to get Eclipse to work (which we need to use in our exam). I'm importing and running some of my programs I had written with gedit and run with command prompt (which worked), but they don't seem to work in Eclipse.
My Hello World program did, but my others seem to throw errors regarding args?

package week1;

public class PersonalGreeting {

    public static void main(String[] args) {
        String first_name = args[0];
        String last_name = args[1];
        System.out.println("Hello " + first_name + " " + last_name);
    }
}

Error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0  
    at week1.PersonalGreeting.main(PersonalGreeting.java:6)

Any ideas how to fix this and what's causing it? Why should it work in command prompt but not Eclipse?

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
Wolff
  • 1,051
  • 3
  • 18
  • 31
  • Are you passing any parameter via Eclipse run configuration ? – Vishal Santharam Feb 07 '14 at 13:48
  • 1
    Hopefully, this teaches you to write your programs in such a way that they never, ever die from missing or invalid command line parameters. Specifically, accessing an array of unknown size is just calling for trouble. – Ingo Feb 07 '14 at 14:14

5 Answers5

2

I would definitely pass it some parameters. You can do this with an eclipse run configuration. Look at the Run menu under Run Configurations. Switch to the Arguments tab and give some program arguments before running again.

EDIT: By the way, when you ask eclipse to run your program without giving it any further details, it creates a default run configuration for the program which then appears in the list of run configurations. I would consider issuing the run command and expecting some error in the output as a common usage pattern with eclipse--just easy to get the run configuration created that way, then go edit it so that it has whatever it needs to be happy as far as program arguments, environment variables, etc.

unigeek
  • 2,656
  • 27
  • 27
2

Eclipse does not pass the parameters as the command line does.

You will need to go to your run configuration (right click the project -> Run As -> Run Configurations and select your application). You would click 'Arguments' and type your arguments in the 'Program arguments' field, then click run.

Vlad Schnakovszki
  • 8,434
  • 6
  • 80
  • 114
  • 1
    That's what I'm looking for! I must admit, it's rather frustrating though... I've gotten used to typing various different inputs in the command line whilst writing it, to see if it works and what I need to change. This seems like quite a tiring process! – Wolff Feb 07 '14 at 18:54
2

That is, because you are not giving any method parameters. When you run your programm from console, you have to enter firstname and lastname, those are missing in eclipse, thus the IndexOutOfBoundsException.

To solve this do following: right-click your project. choose run as. go to run configurations. Enter the first name and last name as Program arguments in the argurments tab.

Kayz
  • 637
  • 1
  • 8
  • 21
1

It looks like the program did not receive the expected command line parameters. Have a look at your execution configuration inside Eclipse in order to pass the mandatory parameters to any command line program you wrote.

Jorge_B
  • 9,712
  • 2
  • 17
  • 22
1
public class PersonalGreeting {

 public static void main(String[] args) {
  String first_name = "";
  String last_name = "";
  if (args.length > 0){
       first_name = args[0];
       if (args.length > 1) last_name = args[1];
  }else{
       first_name = "parameters";
       last_name = "are missing";
  }
  System.out.println("Hello " + first_name + " " + last_name);
 }
}

try to catch errors

tr4pt
  • 317
  • 3
  • 15