Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run a Simple Java Program in Eclipse?

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

like image 590
Robert777 Avatar asked Sep 22 '12 18:09

Robert777


People also ask

How do I run a Java program in Eclipse?

Step 1: Open Eclipse and click File > New > Java Project. Step 2: Provide the Project Name and click on the Finish button. Step 3: In the Package Explorer (left-hand side of the window) select the project which you have created. Step 4: Right-click on the src folder, select New > Class from the submenu.

How do I compile a single Java file in eclipse?

Your class has to define a public static void main(String[] args) method. Then find the Java file in the project explorer, right-click on it, and choose "run as Java application". The file name has to be the same (+ extension ".


5 Answers

  • 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"
like image 89
Alexander Egger Avatar answered Oct 12 '22 23:10

Alexander Egger


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.

like image 38
rokonoid Avatar answered Oct 12 '22 23:10

rokonoid


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.

like image 25
Abhinav Sarkar Avatar answered Oct 13 '22 01:10

Abhinav Sarkar


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.

like image 35
Code-Apprentice Avatar answered Oct 13 '22 00:10

Code-Apprentice


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

Shortcut to Run a class is:

Ctrl + F11

like image 33
Afee Avatar answered Oct 12 '22 23:10

Afee