Problems when trying to compile a Hello World with Eclipse
I'm having problems when trying to compile the follo开发者_如何学JAVAwing code:
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" ?
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.
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
Rename main as HelloWorldApp so that your app becomes HelloWorldApp.java.
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.
精彩评论