File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes 7 errors for beersong source code in HFJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "7 errors for beersong source code in HFJ" Watch "7 errors for beersong source code in HFJ" New topic
Author

7 errors for beersong source code in HFJ

craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
Hi. I've copied the code for the beer song in head first java but command prompt showing 7 errors it seems to be indicating that there's a problem with the " . " between "out" and "printIn"

I've gone over the code and it looks like I've copied it corrrectly.

Here is the error

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\craig>cd C:\Program Files\Java\jdk1.6.0_21\bin

C:\Program Files\Java\jdk1.6.0_21\bin>javac beerbottles.java
beerbottles.java:1: class BeerSong is public, should be declared in a file named
BeerSong.java
public class BeerSong {
^
beerbottles.java:12: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn (beerNum + "" + word + "of beer on the wall");
^
beerbottles.java:13: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn (beerNum + "" + word + "of beer.");
^
beerbottles.java:14: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn ("take one down.");
^
beerbottles.java:15: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn ("pass it around.");
^
beerbottles.java:19: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn(beerNum + "" + word + "of beer on the wall");
^
beerbottles.java:21: cannot find symbol
symbol : method printIn(java.lang.String)
location: class java.io.PrintStream
System.out.printIn ("No more bottles of beer on the wall");
^
7 errors

C:\Program Files\Java\jdk1.6.0_21\bin>

The "^" points to the "." between "out" and "printIn" on the command prompt screen and not the "S" of system as it shows above if that helps


This is my source 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.printIn (beerNum + "" + word + "of beer on the wall");
System.out.printIn (beerNum + "" + word + "of beer.");
System.out.printIn ("take one down.");
System.out.printIn ("pass it around.");
beerNum = beerNum - 1;

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


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

1) The first error tells you that the class "BeerSong" needs to be in a file named "BeerSong.java". Do that.

2) It's println() -- pee arr eye en tee ell en.

That oughta do it!


[Jess in Action][AskingGoodQuestions]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

It is usually not a good idea to store your Java files in your Java directory. Just create a projects directory or something like that in your user directory and add pathToYourJavaInstall\bin to your path environment variable. Then you can compile anywhere.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
Thanks for the replies, I've changed my "eye" to an "ell" and moved the file to C:\Users\myjava

Now showing this error message

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\craig>cd C:\Program Files\Java\jdk1.6.0_21\bin

C:\Program Files\Java\jdk1.6.0_21\bin>javac BeerSong.java
javac: file not found: BeerSong.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Program Files\Java\jdk1.6.0_21\bin>

I'm not sure if my environment settings are correct.

System variables

JAVA_PATH=C:\Users\myjava

PATH="C:\Program Files\Java\jdk1.6.0_21\bin";%PATH%;%CommonProgramFiles%\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\QuickTime\QTSystem\

Not sure how to do this

add pathToYourJavaInstall\bin to your path environment variable. Then you can compile anywhere.


Could you write an example please.

craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
Also tried C:\Users\myjava

But that brought up this error message. Not sure which way is correct, this one or the previuos post.

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\craig>cd C:\Users\myjava

C:\Users\myjava>javac BeerSong.java
BeerSong.java:1: error while writing BeerSong: BeerSong.class (Access is denied)

public class BeerSong {
^
1 error

C:\Users\myjava>

Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 193

There should be a CLASS_PATH in your env settings.

CLASS_PATH = .;C:\Program Files\Java\jdk1.6.0_21\jre\lib;

Dot(.) indicates the current working directory


SCJP 5, SCWCD 5
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
It's called CLASSPATH, not CLASS_PATH.
craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
ok thanks,

Mine is showing

CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

Is this ok, there's also

QTJAVA=C:\Program Files\Java\jre6\lib\ext\QTJava.zip
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You should add the path of where your files are kept, like so:

CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Users\myjava
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

In my opinion you should not set a a classpath environment variable. Because that setting will work for all your projects. You should prefer the
per project setting e.g. javac -cp .:pathToLib ClassName.java

Your path looks good although I find the ;%PATH%; part a bit weird. Now you're include the path in the path. Maybe you should remove that part. I'm not sure.
If it's set correctly then you should be able to do "java -version" from any directory.

That access denied is weird. Apparently you don't have write rights there. Maybe your directory or class file is readonly.

craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
Ok I got the code to compile

The things I changed were I got rid of the %PATH% from the system variables and opened a new folder C:\craigsjava (I think it might have been the user profile that giving me the access denied error).

I have not changed the CLASSPATH environment variable at this point, mainly because the book HVJ tells me I shouldn't have to.

Not entirly sure how to run the JVM

I typed this into command prompt

C:\craigsjava>java BeerSong.class

and got this message

Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.

C:\Users\craig>java BeerSong.class
Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong/class
Caused by: java.lang.ClassNotFoundException: BeerSong.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: BeerSong.class. Program will exit.

C:\Users\craig>

Don't really know what to try next, is it the CLASSPATH variable thats wrong?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

To compile something you type:
javac TestClass.java
And to run:
java TestClass
Note no .class
TestClass needs to have a main method to be able to run it.
craig connell
Greenhorn

Joined: Sep 21, 2010
Posts: 9
Thats got it, Thanks very much
Paul Ward
Greenhorn

Joined: Jan 28, 2011
Posts: 4
I'm decided to try replying to this message because it follows very closely the issue I'm having. I will include my code for the BeerSong, although I'm having the same issue with two other files (one of which is a simple HelloWorld I ran to try and narrow down possible errors. I am able to compile the code (producing my *.class file). I have tried running it from the command line with and without the ".class" on the end. After running it, I get the same error message Craig was getting. I get that it is saying I need a main method, but unless I am crazy, the main method is right up there at the top just beneath the class. I was also able to run a "java -version" and a "javac -version" so my path seems to be properly set. Any help would be greatly appreciated. I have a feeling I might have some greater confidence once my programs successfully run!

The source code:


The error from the Command Line after running "java BeerSong":
C:\Users\Wardzyrama\Desktop>java BeerSong
Exception in thread "main" java.lang.NoClassDefFoundError: BeerSong
Caused by: java.lang.ClassNotFoundException: BeerSong
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: BeerSong. Program will exit.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Use the dir command to confirm that the BeerSong.class file is in your current directory.
If it is, then tryIf that works, read what Wouter Oet said about the classpath variable. You are usually better off with no system classpath at all. There are a few applications, eg QuickTime (I think), which create their own classpath, and will cause errors like what you are suffering to appear mysteriously. If you have any of those applications, you may need to retain your system classpath, but add .; (dot-semicolon) to the beginning of the classpath, and try again.

If the .class file isn't in your current directory, you have a different sort of problem.
Paul Ward
Greenhorn

Joined: Jan 28, 2011
Posts: 4
Sheriff Ritchie,
Thank you so much! The file was indeed in the proper directory and the copy command allowed it to work. After checking the CLASSPATH, the entry was: C:\Program Files\Java\jre6\lib\ext\QTJava.zip

This caused me to note that under the Java folder beneath Program Files, I have three different folders: jdk1.6.0_23, jdk1.6.0_22, and JRE6. Should I delete all but the *23 or just add the "dot-semicolon" to the CLASSPATH. I should say the different versions might be because I'm working through two books, Head First Java which suggests learning through the command line and Big Java which suggests the NetBeans IDE.

I seriously appreciate the help. I feel like I've been spinning my wheels.
Paul Ward
Greenhorn

Joined: Jan 28, 2011
Posts: 4
I should say I tried the "dot-semicolon" approach and it did not work.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Please print your current classpath. I am surprised adding .; didn't work. Did you introduce any spaces?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
Paul Ward wrote: . . . the copy command allowed it to work. . . .
the -cp tag doesn't mean "copy"; it means "classpath".
Paul Ward
Greenhorn

Joined: Jan 28, 2011
Posts: 4
OMG!! I just worked out another program and it ran. I then tried running BeerSong and it ran. So the "dot-semicolon" approach did work. I think what happened is after adding it I had closed my cmd window and re-opened it to a different directory.

I feel like the greenest of greenhorns! Thanks Campbell for all your help. At least now I can see these puppies run after stacking the code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39395
    
  28
No, that is something we all forget; your new PATH and CLASSPATH only take effect in a new command line or terminal window. That applies both to Windows and *nix systems.

And . . . you're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 7 errors for beersong source code in HFJ