aspose file tools*
The moose likes Beginning Java and the fly likes working with book, code compiiles but won't run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "working with book, code compiiles but won Watch "working with book, code compiiles but won New topic
Author

working with book, code compiiles but won't run

illovich (aka p-marx hanley)
Greenhorn

Joined: Jul 25, 2005
Posts: 2
Hey there,

It would be fair to call me a java newb. I've been working with this book "Head First Java" trying to learn java pretty quickly for work, and I ran into a problem that I can't figure out.

Couple notes: I'm working on a mac (OS X 10.4.2) with Eclipse (because that's the environment we use) for coding, but like the book suggests I'm manually switching to a terminal and compiling with javac and running the compiled file with java.

What's weird is, just trying one of the programs in the book I can compile fine (compiles with no errors), but I get an error when I try to run it:

[dhcp230:~/Dev/GoodDog] illovich% java GoodDog
Exception in thread "main" java.lang.NoSuchMethodError: main


I do have a main method, and I've looked over the code a bunch of times, and I'm not using an undeclared method in main... so what's going on? =)

A followup I have, is there any way to get better debugging from java as to where the problem is? I'm figuring there might be something dumb that I did, but all it tells me is the problem is in the main thread, which I figure means the main block in the GoodDogTestDrive class.

Thanks for any help,

ill

Here's the program (it's short):

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();
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();
}

}
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
Originally posted by illovich (aka p-marx hanley):
Hey there,

It would be fair to call me a java newb. I've been working with this book "Head First Java" trying to learn java pretty quickly for work, and I ran into a problem that I can't figure out.

Couple notes: I'm working on a mac (OS X 10.4.2) with Eclipse (because that's the environment we use) for coding, but like the book suggests I'm manually switching to a terminal and compiling with javac and running the compiled file with java.

What's weird is, just trying one of the programs in the book I can compile fine (compiles with no errors), but I get an error when I try to run it:

[dhcp230:~/Dev/GoodDog] illovich% java GoodDog
Exception in thread "main" java.lang.NoSuchMethodError: main


I do have a main method, and I've looked over the code a bunch of times, and I'm not using an undeclared method in main... so what's going on? =)

A followup I have, is there any way to get better debugging from java as to where the problem is? I'm figuring there might be something dumb that I did, but all it tells me is the problem is in the main thread, which I figure means the main block in the GoodDogTestDrive class.

Thanks for any help,

ill

Here's the program (it's short):

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();
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();
}

}


I think it means exactly what it says, there is no main() method in the class, the GoodDog class that is. Why don't you try "java GoodDogTestDrive"?


-Rich, SCJP 1.4
jin sun
Ranch Hand

Joined: Feb 16, 2005
Posts: 30
Yea, Ric's suggestion probably will do the trick.

"java GoodDogTestDrive"
Dhananjay Ramakrishnappa
Greenhorn

Joined: Jul 25, 2005
Posts: 5
I think u have saved both methods in the same source file..

I think u saved the filename as GoodDog.java rather than GoodDogTestDrive.java

If u have saved the file as GoodDog.java u will get an error as what u have got now...

When u run the java command The class name should match with the class with the main() method...Otherwise such an exception is thrown

Try this
illovich (aka p-marx hanley)
Greenhorn

Joined: Jul 25, 2005
Posts: 2
Originally posted by Richard Anderson:

I think it means exactly what it says, there is no main() method in the class, the GoodDog class that is. Why don't you try "java GoodDogTestDrive"?


Yikes!

I'm glad I prefaced my post with "it would be fair to call me a java newb"

I had no idea you could do that. I thought that typing java GoodDog was like saying execute the file GoodDog.class with Java. Apparently I can invoke java to run any class contained within a compiled .class file that's in the current dir.

Java is apparently really cool. Why didn't anyone tell me before?

Thanks for the reply!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Actually, have a look: each class ends up in its own individual .class file -- in other words, one .java file may be compiled into multiple .class files.

You may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Free-verse poetry isn't going to work, nor will "handles" or joke names. Illovich Marx, or Marx Hanley, or Illovich Hanley, or Marx Illovich, or any variation thereon would be fine. You can change your display name
here.
We take this rule rather seriously. Thanks for your cooperation!


[Jess in Action][AskingGoodQuestions]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Welcome to JavaRanch! While you are happy, "Illovich", I'll use the opportunity to call you on your choice of displayed name. Please take a few minutes to read our JavaRanch Naming Policy and then change your displayed name to conform with it. You can change it via the My Profile link.

Thanks,
-Barry


(NR)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
See, the heavy mob is here...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: working with book, code compiiles but won't run