• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First java chap 5 doesn't compile  RSS feed

 
Nicole Munneke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nicole Munneke
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!