devoured elysium
devoured elysium

Reputation: 105227

Problems when trying to compile a Hello World with Eclipse

I'm having problems when trying to compile the following code:

alt text

I first tried compiling with this code:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}

it works as it should. Now, if I try to replace the class name for any other name, it won't work anymore, as it seems to always look after HelloWorldApp. I made sure the file is being saved and so, I even reopened Eclipse. Still the same error. Maybe this is a common problem, with a small work-around?

Thanks


edit: I see what you guys mean, but why does it work when I have the file name as "Main.java" and a class name of "HelloWorldApp" ?

Upvotes: 0

Views: 273

Answers (4)

MikeC
MikeC

Reputation: 408

When changing the name of your class use the REFACTOR option - If you try to edit the name manually yourself the Eclipse Project loses track of your objects.

Upvotes: 0

Warrior
Warrior

Reputation: 39404

Rename main as HelloWorldApp so that your app becomes HelloWorldApp.java.

Upvotes: 1

crunchdog
crunchdog

Reputation: 13478

This is how Java works. Class must have the same name as the file. So the filename for the class MyApp, must be MyApp.java

Upvotes: 2

Robert Campbell
Robert Campbell

Reputation: 6958

You need to rename your .java file to match the class name.

Eclipse will rename your .java file to match your class name automatically if you use its refactoring support. Right click on the class name, hover over Refactor, and select the Rename option. Now when you rename your class, Eclipse will automatically rename your .java file to match.

Upvotes: 6

Related Questions