user407589
user407589

Reputation: 81

Cannot run "Hello World" program in Eclipse

I have an error in my first step with Java, so when i try to run the code hello world:

class apples{
    public static void main(String args[]){
        System.out.println("Hello World!");
    }
}   

I go to: - Run as .. -> Then i choose Java aplicacion - > And i press Ok

But when i press Ok does not appear the window down to show me the correct message Hello World

Upvotes: 2

Views: 4315

Answers (7)

Andreas Dolk
Andreas Dolk

Reputation: 114847

The only problem that causes your error here is that the classname and the filename do not match - and they have to.

Solution

Rename either the file thesame.java to apple.java or the class to thesame. Then if you select "Run as..." again, eclipse will present a menu item to start your Java application.

(other mentioned, that there's no requirement that a top-level class and the filename do match - unless the top level class is public. Of course this is true. But the problem was about "running" a class under eclipse as a Java application)

Upvotes: 2

Cristian Sanchez
Cristian Sanchez

Reputation: 32177

Class names must be capitalized... so change apples to Apples. Also, if you are a beginner (which it seems like), I would recommend the Netbeans IDE -- it's a bit more friendlier for new users than Eclipse.

Upvotes: 0

Logan
Logan

Reputation: 2515

You have 2 classes by name of "thesame.java" under the source folder. Since one is directly under the src folder, and other under (default package), they use the same namespace, hence Interpreter is confused which java file to execute and is asking you to select the class you want to execute.

Upvotes: 0

duffymo
duffymo

Reputation: 309028

Your code works fine for me:

class apples
{
    public static void main(String args[])
    {
        System.out.println("Hello World!");
    }
}

I downloaded it to c:\temp\apples.java.

Here's how I compiled and ran it:

C:\temp>javac -cp . apples.java

C:\temp>dir apples
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

C:\temp>dir ap*
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

08/15/2010  09:15 PM               418 apples.class
08/15/2010  09:15 PM               123 apples.java
               2 File(s)            541 bytes
               0 Dir(s)  107,868,696,576 bytes free

C:\temp>java -cp . apples
Hello World!

C:\temp>

Your lack of understanding and the IDE appear to be impeding your progress. Do simple things without the IDE for a while until you get the hang of it. A command shell and a text editor will be sufficient.

Sorry about missing javac; cut & paste error.

Upvotes: 8

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

If you look at the screenshot, your class name is there, last in the list. Select it and press OK. To not see this message again, right-click on the class name on the left side and select there Run...->Java Application.

Upvotes: 3

Borealid
Borealid

Reputation: 98559

  • You class must be named "thesame" if you store it in a file called "thesame.java", as you have. Either rename your class to "thesame" or change the file to be "apples.java".

  • You should move the "[]" to be before "args". So, String[] args.

  • Either select "apples" at the bottom of the menu you posted and run it, or right-click on the Java file and make it the default thing to run for this project. Or launch it by right-clicking on the file and selecting "run".

Upvotes: -2

Raptrex
Raptrex

Reputation: 4115

Try public class apples and make sure the file is apples.java. Also it should be public static void main(String[] args)

Upvotes: 0

Related Questions