java.lang.NoClassDefFoundError

user588389 picture user588389 · Apr 7, 2011 · Viewed 48k times · Source

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?

Answer

Mark Peters picture Mark Peters · Apr 7, 2011

Don't include the .class in the command:

java GoodDogTestDrive