How to Run a Simple Java Program in Eclipse?

82,593

Solution 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"

Solution 2

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

enter image description here Then you will get a window. Like-

enter image description here

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

And then Click on Apply button and Run Button.

Solution 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.

Solution 4

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

Shortcut to Run a class is:

Ctrl + F11

Solution 5

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.

Share:
82,593
Robert777
Author by

Robert777

Updated on July 10, 2020

Comments

  • Robert777
    Robert777 almost 4 years

    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

    • Robert777
      Robert777 over 11 years
      Okay, Thanks a lot guys !! It's working !