Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working with book, code compiiles but won't run

 
illovich (aka p-marx hanley)
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
jin sun
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, Ric's suggestion probably will do the trick.

"java GoodDogTestDrive"
 
Dhananjay Ramakrishnappa
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, the heavy mob is here...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic