aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java Chapter 2 - Cannot Compile and Run Sample Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java Chapter 2 - Cannot Compile and Run Sample Code" Watch "Head First Java Chapter 2 - Cannot Compile and Run Sample Code" New topic
Author

Head First Java Chapter 2 - Cannot Compile and Run Sample Code

Geoffrey Kidd
Greenhorn

Joined: Feb 23, 2009
Posts: 2
I have java 1.5 installed. If I attempt to run "java GameLauncher" using the GameLauncher.class as provided by the download, I get an exception stack "Exception in thread "main" java.lang.NoClassDefFoundError: GameLauncher (wrong name: chap2/GameLauncher).

If I erase all the *.class files and attempt to compile, javac Player.java gives me a "Player.class" file,
but then running "javac GuessGame.class" gets me a whole bunch of "Cannot find Symbol" errors class Player.

Any help appreciated. Thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch

I haven't used those files recently, but if I remember correctly the downloaded files have package declarations in. So the files are not where you expect them to be.
When you create a file by hand and compile it, when beginning you tend to miss out the package declarations. So you get a MyClass.class file in your present directory, which you might execute with

java MyClass

If the files have a package declaration, then they should be in a folder with the same name as the package; some books use the chapter number as the package name. So your file ought to be in a folder called ch_02 or similar. Then you would have to execute it by writing

java ch_02.MyClass

[Note the . not /, and you have to be in the parent directory where ch_02 is a sub-directory.]
Head First Java is designed to work with Java5.

If you try to compile GuessGame with package declaration, then the compiler will expect that all the other .class files are already there.
I can't remember whether GuessGame requires packages; I think it will work without package declarations. Try copying all the .java files into a guess directory, navigate to that directory and compile again. In this case "cannot find symbol" means there is a .class file missing. If all the .java files are in the same directory the compiler can probably create all the requisite .class files if you say

javac GuessGame.java

I hope that helps more than it confuses
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
And it's "javac GuessGame.java" not "javac GuessGame.class".
Geoffrey Kidd
Greenhorn

Joined: Feb 23, 2009
Posts: 2
Thank you. Commenting out the package statements got me going. I'll have to check the documentation on how to use them later.

Apologies for the mis-typing of "GuessGame.class". Came at the end of a very long day.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome

And we do notice mispellings. For anything of any length use ctrl-c ctrl-v; as you will see here, we may believe people's posted code and assume the spelllling errors are the cause of the problem.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Head First Java Chapter 2 - Cannot Compile and Run Sample Code