• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't run code examples downloaded from website.  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this is the right place to ask this, but I downloaded the code examples from HeadFirst Java. I thought it would save time rather than type the code from the book in order to fool with it if I wanted. But when I tried to run it. I get an error message, like this one for the GuessGame example in chapter 2:


Exception in thread "main" java.lang.NoClassDefFoundError: GameLauncher (wrong name: chap2/GameLauncher)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Has anyone else had this happen to them and could they explain to me what's going on? If copy the code from the book then it runs. I could very well be missing something simple, but I don't know what that could be. Do I have to compile them again or something? Sorry if it's a stupid question, but I'm just getting back into this.

The code is in a directory that looks like this, if this helps to know it:

AppendixA chap03 chap06 chap09 chap12 chap15
chap01 chap04 chap07 chap10 chap13 chap16
chap02 chap05 chap08 chap11 chap14 chap18

Any suggestions on how I could get these programs to run?




 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you copy from book it runs, and downloaded code doesn't run?
What do you mean? Please give more details.

Where did you download to? What package you copied the class to? Where do do run from it? command line? If so, what command did you try?
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I downloaded it to my downloads folder on my MB pro. I run it from the Terminal application.

I do what I always do -- I go to the directory containing the code (in this case cd/Downloads/hfjavafinalsamples/chap02/ then type java GameLauncher. Here's the list of the code that's in the file, if that helps:

GameLauncher.class GuessGame.class Player.class
GameLauncher.java GuessGame.java Player.java


Although I didn't copy anything to a package -- i just downloaded the code. I never did anything with a package (haven't learned about those yet) when I copied it from the book.

I'm not sure what else to say. But like I said, when i copy the code from the book, it runs, when I downloaded it, nada. So I'm thinking that there's something about the download that's affecting it.
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:


 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the problem is shown in the error message: “wrong name”.
There is a subtle difference between the code in the book and the code you downloaded: the downloaded .java file becgins like this:-The .class file needs to be inside a folder called chap02, exactly the same as the name of the package.
You need to navigate to the folder one “above” chap02; if you get chap02 printed out when you list your directory, you are in the right place.
You execute it with this instruction:-
java chap02.GameLauncher
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Confirm that by opening the appropriate .java file. If you look in the book, you won't see the package name; it is missed out. I unzipped the wickedlysmart file and it said package chap02; in the game launcher Java file.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that and it still doesn't execute; same error messages. And the package code line is there.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good grief!

Tried the same, and got the same error message. You know what has happened? My underlining.
campbell@XXX:~/java/hfj/hfjavafinalsamples/chap02> javap -c GameLauncher
Warning: Binary file GameLauncher contains chap2.GameLauncher
Compiled from "GameLauncher.java"
public class chap2.GameLauncher {
public chap2.GameLauncher();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #16 // class chap2/GuessGame
3: dup
4: invokespecial #18 // Method chap2/GuessGame."<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #19 // Method chap2/GuessGame.startGame:()V
12: return
}

They compiled that .class file and then changed the .java file afterwards. The .class file has chap2 in and the .java file has chap02
The only thing to do is to recompile the .java files
campbell@XXX:~/java/hfj/hfjavafinalsamples> javac chap02/*.java
campbell@XXX:~/java/hfj/hfjavafinalsamples> java chap02.GameLauncher
I'm thinking of a number between 0 and 9...
You may have to repeat the procedure for all the other chapters. I haven't heard of that erratum before. Consider going here to report it.

Again: good grief!
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works now -- many thanks!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!