8

I'm learning Java am having trouble running an example program.

I have two files:

GoodDog.java:

 class GoodDog { private int size; public int getSize() { return size; } public void setSize(int s) { size = s; } void bark() { if (size > 60) { System.out.println("Wooof! WoooF!"); } else if (size > 14) { System.out.println("Ruff! Ruff!"); } else { System.out.println("Yip! Yip!"); } } } 

GoodDogTestDrive.java:

 class GoodDogTestDrive { public static void main (String[] args) { GoodDog one = new GoodDog(); one.setSize(70); GoodDog two = new GoodDog(); two.setSize(8); System.out.println("Dog one: " + one.getSize () ); System.out.println("Dog two: " + two.getSize () ); one.bark(); two.bark(); } } 

They are typed out exactly the way they are in the book and compile without issue. When I try to run GoodDogTestDrive I get this:

nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class java.lang.NoClassDefFoundError: GoodDogTestDrive/class Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ 

What am I doing wrong?

2
  • I'm looking for the generic question which answers questions of this nature, they are extremely common and you should be able to figure it out by looking at a related post. If anybody beats me to the URL please post it. Commented Apr 7, 2011 at 21:03
  • 4
    Every time you put a space next to the parens, I cringe. Commented Apr 7, 2011 at 21:09

7 Answers 7

16

Don't include the .class in the command:

java GoodDogTestDrive 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it works! I wonder why removing .class makes it work... I'll have to google it.
I made that same mistake when I started through that book ;)
@is_shields that's because a . is a scope resolution operator in Java, so GoodDogTestDrive.class refers to the class named class of the package GoodDogTestDrive, which of course doesn't exist. This is why your error message has GoodDogTestDrive/class in it
14

It is important to note that while the resolution was simple for this problem case (simply removing .class from the Java command line), java.lang.NoClassDefFoundError can be hard to pinpoint in the context of Java Web application developments.

This Java exception means that a “runtime” Java class could not be loaded and/or found in the current Thread context classloader. This is normally the results of:

  • Missing library in your runtime classpath.
  • Static {} block code initializer execution failure (preventing class loading of the affected class).
  • JAR file packaging / class loader tree problems such as mix of JAR files deployed at the child & parent class loader.

It is recommended for any Java beginner to properly understand this type of problem in anticipation of future troubleshooting episodes.

Comments

1

just run the program with "java yourClass".

Do not use .class in the end.

Try it and let us know.

Comments

0

If the GoodDog class file is not located at the current working directory you will need to set the classpath on your java command....

java GoodDogTestDrive -cp ./path/to/gooddog/ 

Comments

0

Issue-java.lang.NoClassDefFoundError

Root Cause: Incorrect Java path set in Environment Variable Section

Solution: Set correct JAVA_HOME Path

Steps->Environment Variable Setting (My Comp-Right Click ->Properties->Env Variable->Advance Tab ->Variable)

  1. Create new JAVA_HOME Environment Variable.

    JAVA_HOME **.;C:\Program Files (x86)\Java\jdk1.6.0_14** 
  2. Set JAVA_HOME variable in PATH Variable section.

    PATH %JAVA_HOME%\bin 
  3. Set JAVA_HOME variable in CLASSPATH Variable

    CLASSPATH %JAVA_HOME%\jre\lib 
  4. Restart System

  5. Verify all variable

    echo %CLASSPATH% echo %JAVA_HOME% echo %PATH% 
  6. Compile java class javac Test.java

  7. Run Java program java Test

Comments

0

Thrown if the JVM or a ClassLoader instance tries to load in the definition of a class (method call or creating a new instance) and no definition of the class could be found.

The reason for NoClassDefFoundError is that a particular class is not available on runtime but was available during compile time.

1.The class belongs to a missing JAR or JAVA file or JAR was not added into classpath.

2.Class is not available in Java Classpath.

3.NoClassDefFoundError in Java due to Exception in Static Initializer block. when your class perform some static initialization in static block, and if static block throw an Exception, the class which is referring to this class will get NoclassDefFoundError in Java.

4.Your Classpath, PATH or JAVA_HOME is not setup properly or JDK installation is not correct. which can be resolved by re-installing JDK.

5.Do a maven clean-install and simply restart your server with necessary Source(.java) files.

Comments

-1

I was facing the same problem as you.

I resolved the problem like this:

  1. Check the class path in evironment variables
  2. I opened the project in navigator of eclipse and I checked the class files. My class files were in different folders not in the bin folder. I just copied the missing files into the bin folder then the problem was resolved.

Simply copying the missing .class files to bin directory of eclipse project resolved the problem.

Comments