user588389
user588389

Reputation:

java.lang.NoClassDefFoundError

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?

Upvotes: 8

Views: 49661

Answers (7)

Lucky
Lucky

Reputation: 17365

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.

Upvotes: 0

laxminarayana
laxminarayana

Reputation: 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.

Upvotes: -1

user2735451
user2735451

Reputation: 1

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

Upvotes: 0

P-H
P-H

Reputation: 385

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.

Upvotes: 14

axcdnt
axcdnt

Reputation: 14624

just run the program with "java yourClass".

Do not use .class in the end.

Try it and let us know.

Upvotes: 1

mmccomb
mmccomb

Reputation: 13817

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/

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 81134

Don't include the .class in the command:

java GoodDogTestDrive

Upvotes: 16

Related Questions