wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Error in he exercise Beersong in the book Head first Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in he exercise Beersong in the book Head first Java" Watch "Error in he exercise Beersong in the book Head first Java" New topic
Author

Error in he exercise Beersong in the book Head first Java

Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Please can someone help me...

I work with windows and try to compile next code in MS DOS wih the command:

= code

public class BeerSong {
public static void main (String[] args) {
int beerNum = 99;
String word = "bottles";

while (beerNum > 0) {

if (beerNum == 1) {
word = "bottle"; // singular, as in ONE bottle.
}

System.out.println(beerNum + " " + word + " of beer on the wall");
System.out.println(beerNum + " " + word + " of beer.");
System.out.println("Take one down.");
System.out.println("Pass it around.");
beerNum = beerNum - 1;

if (beerNum > 0) {
System.out.println(beerNum + " " + word + " of beer on the wall");
} else {
System.out.println("No more bottles of beer on the wall");
} // end else
} // end while loop
} // end main method
} // end class

= code

But now I get the error:

class Beersong is public, should be declared in a file named Beersong.java

How can I solve this problem?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
Read the error message. Call the file you are compiling BeerSong.java.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
And please always use the Code button.
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Friends,
I changed the name of the textfile but now I have another question:

I got the next message:


D:\Java\bin>javadoc BeerSong.java
Loading source file BeerSong.java...
Constructing Javadoc information...
Standard Doclet version 1.6.0_21
Building tree for all the packages and classes...
Generating BeerSong.html...
Generating package-frame.html...
Generating package-summary.html...
Generating package-tree.html...
Generating constant-values.html...
Building index for all the packages and classes...
Generating overview-tree.html...
Generating index-all.html...
Generating deprecated-list.html...
Building index for all classes...
Generating allclasses-frame.html...
Generating allclasses-noframe.html...
Generating index.html...
Generating help-doc.html...
Generating stylesheet.css...

I Have no class in the bin directory of the BeerSong.
When I give the next command: java BeerSong
I get an error:


Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong
then a view messages and then: Could not found the main class: BeerSong . Program will exit

Can someone explain what I do wrong.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Frank Greenhorn wrote:Friends,
I changed the name of the textfile but now I have another question:

I got the next message:


D:\Java\bin>javadoc BeerSong.java
Loading source file BeerSong.java...
Constructing Javadoc information...
Standard Doclet version 1.6.0_21
Building tree for all the packages and classes...
Generating BeerSong.html...
Generating package-frame.html...
Generating package-summary.html...
Generating package-tree.html...
Generating constant-values.html...
Building index for all the packages and classes...
Generating overview-tree.html...
Generating index-all.html...
Generating deprecated-list.html...
Building index for all classes...
Generating allclasses-frame.html...
Generating allclasses-noframe.html...
Generating index.html...
Generating help-doc.html...
Generating stylesheet.css...

I Have no class in the bin directory of the BeerSong.
When I give the next command: java BeerSong
I get an error:


Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong
then a view messages and then: Could not found the main class: BeerSong . Program will exit

Can someone explain what I do wrong.


Did you compile the class? The command would be javac BeerSong.java.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
David Patrzeba
Greenhorn

Joined: Jun 08, 2010
Posts: 14
Did you compile BeerSong.java.

It looks like you built the java documents.

You need to use the command javac BeerSong.java
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
Thank you that was indeed the solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in he exercise Beersong in the book Head first Java
 
Similar Threads
Beersong
BeerSong Java code
Head first Beersong
head first beersong problem?
7 errors for beersong source code in HFJ