• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner - problems running code  RSS feed

 
Zach Heckman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working through Head First Java but already having trouble on the first code example: 99 bottles of beer. I have successfully compiled and run a Hello World program, and can compile the 99 Bottles program but cannot run it. (Tried commands "java BeerSong" and "java -classpath . BeerSong")

Error: Could not find or load main class BeerSong

I checked that the class name is BeerSong.





screen1.jpg
[Thumbnail for screen1.jpg]
screen2.jpg
[Thumbnail for screen2.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, the name of the class is "chap01.BeerSong"... which is why the JVM can't find the BeerSong class. Second, you have to put the class files in a "chap01" subdir from the classpath root.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 1 you have the package as chap01. This means that your source file, BeerSong.java, should be in a directory named chap01. You would then compile and run the program from the parent directory of chap01.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

You will find it a lot easier if you miss out the package names for the first few weeks. Prefix the line package chap01; with // and the name of the class will be BeerSong plain and simple.
It is a bit confusing because the paper copy of the book lacks package names, but the downloaded classes do have package declaration.
 
Zach Heckman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it thank you and thanks for the welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!