File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes code from the Head-First-Java Book don't run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "code from the Head-First-Java Book don Watch "code from the Head-First-Java Book don New topic
Author

code from the Head-First-Java Book don't run

Jeremy James Brown
Greenhorn

Joined: Mar 24, 2011
Posts: 27
So I registered here because I bought the book Head First Java.

I wanted to start practicing so I typed up code given in the first part of the book. It compiles to a class but the class does not run.

For instance.


Is from page 23, I compiled the text to Test.class and when I run it from cmd it receives an error;

Exception in thread "main" java.lang.NoClassDefFoundError: Test/class

I'm new to OO programming. Why is the code from this book not running? Otherwise I love the book, but this is the third straight sample code that compiles but doesn't run.

Thanks!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

When you run the program, you must enter the command:

java Test

Note: Not java Test.class. You're not specifying the filename there, but the name of the class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Jeremy James Brown wrote:... Exception in thread "main" java.lang.NoClassDefFoundError: Test/class...

When you enter the command to run your compiled class file, do not include the ".class" extension. Just type...

java Test


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Doh! Jesper beats me by 13 seconds!
Jeremy James Brown
Greenhorn

Joined: Mar 24, 2011
Posts: 27
Jesper de Jong wrote:When you run the program, you must enter the command:

java Test

Note: Not java Test.class. You're not specifying the filename there, but the name of the class.


Thanks, had already tried that. I'll make sure I do not type class in the future. But without .class I still get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Jeremy James Brown
Greenhorn

Joined: Mar 24, 2011
Posts: 27
I booted up my Ubuntu laptop and the code compiled on the win box runs properly. I must have a problem with my windows java.

javac compiles on the win box. java returns the Usage: java text.

This is weird.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Jeremy James Brown wrote: ...without .class I still get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: Test
...

In that case, it looks like your Windows machine has a system/user variable set for "classpath" that does not include a dot for the current directory. Try changing the current directory to the location of your compiled Test.class file, then provide a command-line classpath (cp) of the current directory (.) by typing...

java -cp . Test

If that works, then it confirms the classpath theory, and you should either remove the system/user classpath entirely (if there's nothing else in that path you need), or at least add a dot to to list of paths.

Edit: A lot of people make the mistake of creating a classpath variable pointing to the jdk bin directory. This directory should be added to your "class" variable, but you do not need a "classpath" pointing to that location.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
I hope it's not too late to welcome you to the Ranch

marc weber wrote:. . . you do not need a "classpath" pointing to that location.
Unless you have a classpath like that shown in your other thread (which had been set up by QuickTime, and needed a . added to it), you probably don't need a classpath at all.
Jeremy James Brown
Greenhorn

Joined: Mar 24, 2011
Posts: 27
marc weber wrote:... then provide a command-line classpath (cp) of the current directory (.) by typing...

java -cp . Test

...


I tried this but kept running into the cmd terminals issues negotiating the "Space" the path past Program Files. Yes Really. MS CMD can not read a Windows 7 default directory path.

I took a leap of faith and deleted the CLASSPATH entries from User and System Variables and now only have JAVA_HOME with the appropriate path.

So far so good. I can now run my .class files in Windows.

Thanks so much!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
If you have a space in a PATH entry, try putting quotes round it

"C:\Program Files\Java\jdk1.6.0_23\bin"
 
 
subject: code from the Head-First-Java Book don't run