• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in he exercise Beersong in the book Head first Java

 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the error message. Call the file you are compiling BeerSong.java.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please always use the Code button.
 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Patrzeba
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that was indeed the solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic