Beginning Java programmer 10.4

Discussion in 'Mac Programming' started by jsprice2, Mar 20, 2007.

  1. jsprice2 macrumors newbie

    Nov 15, 2006
    java beginner - OS 10.4 - using terminal and Head First Java text to self learn - old BASIC person from years ago -

    get the following error:
    Exception in thread "main" java.lang.NoClassDefFoundError: GoodDogTestDrive/class

    code below - would also like to know how to run code with 2 classes - everything is in the same directory

    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("Woof Woof !");
    } else if (size > 14) {
    System.out.println("Ruff Ruff!");
    } else {
    System.out.println("Yip Yip!");

    class GoodDogTestDrive {
    public static void main (String[ ] args) {
    GoodDog one = new GoodDog();

    GoodDog two = new GoodDog();

    System.out.println("Dog one: " + one.getSize());
    System.out.println("Dog two: " + two.getSize());

  2. lazydog macrumors 6502a

    Sep 3, 2005
    Cramlington, UK
    Is your file named

    b e n

    EDIT: I've put all the code you posted in a file called and it compiles and runs fine.
  3. cbougher macrumors member

    Oct 19, 2006
    Atlanta, GA
    I think GoodDogTestDrive needs to be declared public.
  4. ChrisBrightwell macrumors 68020


    Apr 5, 2004
    Huntsville, AL
    Don't include the ".class" in your java command.

    For example, to build and run

    class HelloWorld
      public static void main (String args[])  
        System.out.println("Hello, world!");
    Then in terminal, these are your commands:
    % javac
    % java -cp ./ HelloWorld
    The problem with using "java HelloWorld.class" is that the . is a package/file separator, so it looks for ./HelloWorld/class.class ... which won't exist. :)

    Also, the "-cp ./" is just to ensure that it looks in the local directory for the .class file to run. Some JVMs add automatically search there, but not all do, so it's usually a good practice to explicitly set the classpath.

    Hope this helps.
  5. scan macrumors 6502

    Oct 24, 2005
    exactly what he said. This is a common exception. If you run your program without the .class then its a classpath issue.
  6. jsprice2 thread starter macrumors newbie

    Nov 15, 2006
    Beginning Java programmer 10.4

    Thanks for alll the help. I did put .class at the end of command line and it was the problem.

Share This Page