How to Run a Simple Java Program in Eclipse?

2019-01-24 07:27发布

问题:

As you can probably understand from the question itself, I'm new to Java. I was given an exercise to write a Java program which receives a character, prints it and the next character in the Unicode table.

Now, I have the solution to this exercise:

public static void main(String[] args){
    char c = args[0].charAt(0);
    char c1 = (char)(c + 1);
    System.out.println(c + "\t" + c1);
}

I understand basic idea of this code, but I'm trying to run this code in Eclipse I get an annoying error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at MainClass.main(MainClass.java:9)

Note: I have yet to run a Java program that actually receives something as a parameter so I guess it's a stupid beginners' mistake... Here is the full code that I tried to compile in Eclipse:

public class MainClass {

    /**
     * @param args
     */

    public static void main(String[] args){
        char c = args[0].charAt(0);
        char c1 = (char)(c + 1);
        System.out.println(c + "\t" + c1);
    }
}

Thanks in advance

回答1:

  • Select "Run -> Run Configurations" from the menu.
  • Search for you project in the list on the left and select it.
  • Select the "Arguments" tab on the right.
  • Write the argument you want to pass to the programm in "Programm arguments".
  • Click "Run"


回答2:

Right click on your java file in project explorer of your eclipse. Then Run As> Run Configuration

Then you will get a window. Like-

Click on Arguments Tabs, and then write some text there, may be a character.

And then Click on Apply button and Run Button.



回答3:

The default run configuration in Eclipse runs a Java program without any arguments, hence the ArrayIndexOutOfBoundsException. Your code is trying to get first element of the args array when there aren't any!

You can edit the run configuration to provide the arguments to run your program with. Then it should not throw this exception.

However, a good practice is to check the size of array before accessing it's elements, more so when the array is coming as an argument from outside of your code.



回答4:

This is a great question with some very good answers. I would like to add some pointers about how to debug your own program. Debugging is as important (if not more important) than writing code.

For one thing, Eclipse has some great debugging features. You can use this debugger to find problems in your code. I suggest that you learn how to use it. In particular, you can set watches for variables to see what value they have as you step through the execution of your code.

Alternatively, you can add calls to System.out.println() to print out the values of any variables. For example, adding the following line at the beginning of your code might help you narrow down the problem:

System.out.println(args[0]);

This would also give an ArrayIndexOutOfBoundsException if no command-line arguments are given. Then you could do something like

System.out.println(args.length);

which would print out 0. This then gives you a clue as to where the problem is.

Of course, even when you get to this point, you still might not know how to solve the problem. This is where sites like StackOverflow come in handy.

Good luck with your Java experience. Please come back when you need more help.



回答5:

If your Run Configurations are in place (as already shown in above answers):

Shortcut to Run a class is:

Ctrl + F11