This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes problem compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem compiling" Watch "problem compiling" New topic
Author

problem compiling

james hargis
Greenhorn

Joined: Jun 18, 2006
Posts: 2
so I am followin the code in head first java and am running into a problem every time i try to compile. I get this error:

C:\java>javac beer.java
beer.java:1: class BeerSong is public, should be declared in a file named BeerSo
ng.java
public class BeerSong {
^
1 error

If I remove the public from the beginning of the line then everything compiles and works fine. Why is this?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Welcome to the Ranch.

Thank you for posting the whole message, which tells you exactly what is wrong. The problme is not that BeerSong is public, the problem is that it isn't in a file called BeerSong.java. Yo can't have more than one public class per file, and the name of the file and the name of the public class have to be the same. Move the BeerSong class into its own file.

And don't worry about the compiler not compiling BeerSong; when it finds a declaration of a BeerSong class in your code, it will go and look for the BeerSong.java file.
james hargis
Greenhorn

Joined: Jun 18, 2006
Posts: 2
ohh ok I see... its because I didnt name the file BeerSong.java. I just named it beer.java. I get it. thanks for the quick reply. I am really new to programing. I have learned a little bit of C++ but not much. I am taking a java class this summer at the local CC and this book looks like it is going to help a lot. I just finished chapter 1 and seem to be getting it so far.

Thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Only too happy to be able to help.
[ June 18, 2006: Message edited by: Campbell Ritchie ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem compiling
 
Similar Threads
Beersong
Head first Beersong
7 errors for beersong source code in HFJ
head first beersong problem?
BeerSong Java code