• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java - Guessing Game Sample Code  RSS feed

 
Dustin Wright
Ranch Hand
Posts: 33
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Please bear with me I'm on my lunch break and have to do this quickly.  I'm working through the book.  In chapter 2 is the guessing game.  I have the sample code from WikedlySmart.com below.  It appears to have been compiled as there are both .java and .class files in the folder.
Sample Code

I tried to run the game from the sample code folder:

C:\Users\nzero\Google Drive\Development\Head First Java\Sample Code\Head-First-Java-master\chap02>java GameLauncher
Error: Could not find or load main class GameLauncher

C:\Users\nzero\Google Drive\Development\Head First Java\Sample Code\Head-First-Java-master\chap02>


I then tried to copy the .java files to an empty folder and recompile it.  That results in this:

C:\Users\nzero\Google Drive\Development\Head First Java\Sample Code\Head-First-Java-master\chap02\Test>javac GameLauncher.java
GameLauncher.java:5: error: cannot find symbol
        GuessGame game = new GuessGame();
        ^
  symbol:   class GuessGame
  location: class GameLauncher
GameLauncher.java:5: error: cannot find symbol
        GuessGame game = new GuessGame();
                             ^
  symbol:   class GuessGame
  location: class GameLauncher
2 errors

I've not modified the code.  What might I be doing wrong?

Thanks.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a GuessGame.java file? If not, that's your problem. If you do then compile all java files:
javac *.java
 
Dustin Wright
Ranch Hand
Posts: 33
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but something is wrong, Compiling it again with the wildcard worked.  It still will not run.  Again, this is the sample code.  I've not changed it.  Other code runs on my machine, so I know it's not my PC.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you run the code?
What command are you using?
What folder are you in?
Are the java classes in packages (do they have a 'package ...;' line at the top)?
 
Dustin Wright
Ranch Hand
Posts: 33
1
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying to help, I have no URL to put a screenshot on.  It's not that big a deal.  It's just the sample code.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because the code in the book and the code on the website differ slightly. The code on the website has package names in (Dave Tolls suspected correctly), so you will find something like
package chapter02;
near the beginning of the file. There are several possible solutions to your problem. If you are only in Chapter 2, you probably want the simple solution: delete the package declaration. Your downloaded file will now look the same as that in the book. You can now compile and run it with the usual javac XYZ.java→java XYZ instuctions.

There are several ways to compile classes from the command line if they have package names. This is one way:-
  • 1: Create a directory called chapter02 or similar: exactly the same as the package name.
  • 2: Move all the XYZ.java files with that package name into that directory.
  • 3: Open a command line/terminal.
  • 4: Navigate to the chapter02 directory's parent directory with the cd instruction. If you use the dir instruction (Windows®) or ls (*nix) you will see the chapter02 directory included in this directory.
  • 5: Compile the chapter02/XYZ.java file: you will probably find javac chapter02/*.java easiest. Note /: on Windows®, you would use \ instead.
  • 6: Execute with java chapter02.XYZ Note dot not slash.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!