• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doh! Jesper beats me by 13 seconds!
 
Jeremy James Brown
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a space in a PATH entry, try putting quotes round it

"C:\Program Files\Java\jdk1.6.0_23\bin"
reply
    Bookmark Topic Watch Topic
  • New Topic