This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

7 errors for beersong source code in HFJ

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
craig connell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>

 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's called CLASSPATH, not CLASS_PATH.
 
craig connell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 14857
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats got it, Thanks very much
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 77947
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should say I tried the "dot-semicolon" approach and it did not work.
 
Campbell Ritchie
Marshal
Posts: 77947
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please print your current classpath. I am surprised adding .; didn't work. Did you introduce any spaces?
 
Campbell Ritchie
Marshal
Posts: 77947
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Ward wrote: . . . the copy command allowed it to work. . . .

the -cp tag doesn't mean "copy"; it means "classpath".
 
Paul Ward
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 77947
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic