简体   繁体   中英

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?

不要在命令中包含.class

java GoodDogTestDrive

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.

just run the program with "java yourClass".

Do not use .class in the end.

Try it and let us know.

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

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.

如果GoodDog类文件不在当前工作目录中,则需要在java命令上设置类路径。

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM