Forums Register Login

Head First java chap 5 doesn't compile

+Pie Number of slices to send: Send
Hi everyone,

I'm starting to learn Java with the Head First Java book (my manager at work gave me the book), but I get stuck at chapter 5.
I write the code in Notepad++ and compile it in commandprompt.
I'm supposed to write the code for two classes, and copy the code for the third class. I can also copy the code for all three classes, leaving me with the same error.
The error: Could not find or load main class SimpleDotCom
Could not find or load main class Game
Could not find or load main class GameHelper

The code is in three different files.
The code:








Can someone please help me out?
+Pie Number of slices to send: Send
The GameHelper class is in the chap5 package and not in the helpers package. That's why you are getting the error. Comment out the import helpers.GameHelper and see what happens
+Pie Number of slices to send: Send
Welcome to the Ranch! Thank you for using code tags and formatting your code.

What command are you using on the command line and what directory are you in when you issue it? Remember that you have to be cd'd to the project root and using the "dot" name for the Java file.
+Pie Number of slices to send: Send
Note also that the class referenced on the commandllne needs to have a main() method for the java program to find to start the program's execution.
Game is the only class I see with a main method.
So the command would be: java chap05.Game
+Pie Number of slices to send: Send
Thanks for your replies, sadly, it still doesn't work.
When trying to compile the Game.java (javac Game.java), I get the following errors:

Game.java:11: error: cannot find symbol
GameHelper helper = new GameHelper();
^
symbol: class GameHelper
location: class Game

Game.java:11: error: cannot find symbol
GameHelper helper = new GameHelper();
^
symbol: class GameHelper
location: class Game

Game.java:13: error: cannot find symbol
SimpleDotCom theDotCom = new SimpleDotCom();
^
symbol: class SimpleDotCom
location: class Game

Game.java:13: error: cannot find symbol
SimpleDotCom theDotCom = new SimpleDotCom();
^
symbol: class SimpleDotCom
location: class Game



I did comment out the import helper.GameHelper, and got these errors when I tried to recompile it into a .class.



I also tried to work with chap05.Game.java, but that doesn't seem to do the trick, because I get the error it can't find the file, or it can not find or load the main class.
+Pie Number of slices to send: Send
If you are in the directory that contains the chap05 directory then the compilation command should be:
javac chap05/Game.java

You use the file path, not dot notation.
That assumes you haven't set a CLASSPATH environment variable (which you shouldn't have done).

If you were in the chap05 directory then you would have to tell javac where the package root is:
javac -cp.. Game.java

That tells javac to use the directory above the one you are in as the classpath.
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 821 times.
Similar Threads
Head First Java (2nd Ed.) p. 112
This code of mine is not working, Please help.... i am studying from Head first java
Hi All, While working with Head first java, I got into a problem, please help me to solve this.....
Please help with game
While working from Head first java, I came across this problem... Please help
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:38:10.