• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overriding - an example gone wrong!  RSS feed

 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks, i've copied a 'simple' example on Method Overriding out of a book, to try out, Verbatim.
But i'm getting an error when I try and run the program.
It compiles ok, but I get the error
java.lang.NoSuchMethodError: main
Exception in thread "main"
when i try and run it.

The name of the Java file is simply called A.java.
The full listing is show below.
Can anybody help me?
What am i doing wrong??
Cheers in advance.
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you typing java A or java Override on the command line?
Angel
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No idea
I'm using the IDE Sun ONE Studio 5 to compile & execute the program.
:roll:
 
Joe Ess
Bartender
Posts: 9429
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Jensen:
Folks, i've copied a 'simple' example on Method Overriding out of a book, to try out, Verbatim.

Same problem as
before. The top-level class is called "A" and it doesn't have a public method called "main". I think if you saved that source as Override.java you might be able to execute it, since Override has a "main" method.
Check and see if that book explains the relationship between Java classes and Java source files. The top-level class, the one whose name matches the source file, is the one that the VM attempts to call public static void main(String args[]) on.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve
In that case rename A.java to Override.java and try...
Regards
Maulin
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have no idea

With all due respect, you need to get an idea! Typing in code and then not knowing what to do when it fails to execute implies that you're getting ahead of yourself. You do have to learn to walk before you learn to run. I very strongly recommend learning to run programs from the command line first, and only moving to an IDE after you understand the fundamentals.
In any event, the error says there's no main method, and so you must be trying to execute some class other than "Override" as an application. The command lines to compile and run this application (assuming it's all in a file named "A.java" would be
% javac A.java
% java Override
It would be worth your while learning how to get things working this way; by the time you did, you'd understand some important stuff.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sussed it.
I renamed A.java to Override.java, and it worked a treat.
How stupid of me.
The silly thing is that I used to do a bit of Java programming, but haven't touched ANY Java, whatsoever, for 2 years.
I've forgotten EVERYTHING.
Guess i've got big problems if I can't even suss such a silly error.
Thanks for your help folks, all of you.
[shuffles out of the bar].
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!