aspose file tools*
The moose likes Beginning Java and the fly likes Program from Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Program from "Head First Java" won Watch "Program from "Head First Java" won New topic
Author

Program from "Head First Java" won't run

david jines
Greenhorn

Joined: Feb 19, 2007
Posts: 2
Why does the following program from page 36 compile but not run? It sets the message: Exception in thread "main" java.lang. NoSuchMethodError:main.

code
--------------------------------------------------------------------------- class Dog {

int size ;
String breed ;
String name;

public void bark() {
System.out.println ("Ruff! Ruff!");
}
}

class DogTestDrive {

public static void main(String[] args) {

Dog d = new Dog() ;

d.size = 40;

d.bark();
}
}
------------------------------------------------------------------------


If I remove the class DogTestDrive line and appropriate braces it then runs but that seems to be defeating the intent of the lesson.
sanjay baba
Greenhorn

Joined: Jan 12, 2007
Posts: 1
i think you are compiling class Dog and trying to run it.....you need to compile DogTestDrive and run the same......
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2061
Hi,

What is the intent on that topic?

Is that the exact code?

How did you compile it? run it?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

Did you save each of these in its own file? Or did you save both of them in a file named DogTestDrive.java ?

When you try to run it, be sure that you type
java DogTestDrive
and not
java Dog

What is the exact error message?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
Class which define main method should be public....make DogTestDrive class as public ...it will execute properly...

FYI


class Dog {

int size ;
String breed ;
String name;

public void bark() {
System.out.println ("Ruff! Ruff!");
}
}

public class DogTestDrive {

public static void main(String[] args) {

Dog d = new Dog() ;

d.size = 40;

d.bark();
}
}


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
suji kumar
Greenhorn

Joined: Nov 08, 2006
Posts: 10
dear harish,
Class which has main method need not be public.
The exception may be due to, David must have tried to run : java DOG , which doesnt have main method , instead of DOGTESTDRIVE
david jines
Greenhorn

Joined: Feb 19, 2007
Posts: 2
Thanks so much for your help. The last program I wrote and ran was either Fortran or Algol back in 1969 so it never crossed my mind to start anywhere but at the first line of the program. Starting with DogTestDrive instead of Dog did the trick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Program from "Head First Java" won't run