Error: Exeption in thread "main" java.lang.NoSuchMethodError: main
Frank Thuring
Greenhorn
Joined: Aug 10, 2010
Posts: 28
posted
0
Please help me
I compile with the command promp in windows the next code:
import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite () {
Frame f = new Frame();
Label I = new Label ("Party at Tim's");
Button b = new Button ("You bet");
Button c = new Button ("Shoot me");
Panel p = new Panel();
p.add(I);
}
}
The compiler has made a class file Party. But when I give the command java Party then I got the error:
Exeption in thread "main" java.lang.NoSuchMethodError: main
Check, there are some other coding should be in the book, try find out it. Because, this is not complete!
Tim Batts
Greenhorn
Joined: Aug 09, 2010
Posts: 10
posted
0
Every program has to have a main() because that is the main method that kicks everything else off. In Head First some of the examples they give are incomplete because they are only intended to show you a particular part of the coding process, especially at the beginning. I think this is one of those cases
Tim Batts wrote:Every program has to have a main() because that is the main method that kicks everything else off. In Head First some of the examples they give are incomplete because they are only intended to show you a particular part of the coding process, especially at the beginning. I think this is one of those cases
who says everyclass should have a main mehtod to run......we can also run the program sucessfully without a main method
see the below code......
it is because JVM search for a static block first.......
and if we exit the static block using System.exit(0);
then it is also perfectly fine...........
but in your case as there is no static block and no System.exit(0); then it would be neccessary to have a main method.....
shanky sohar wrote: . . . we can also run the program sucessfully without a main method . . .
. . . but only as a party trick
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted
0
Also, it's important to differentiate between a class and an application. An application you intend to run from the command line needs to have a main method in one of its classes, but all but the most trivial example applications will have more than one class.
I think you are running it in eclipse that the problem you are getting.
so do this thing.
open Run configuration->
on left hand side you will find "Java Application" double click on that.
then on right hand side in main class box write your file name which is according to your post as"Party".
click on apply on the same window.and then click on run on same window.
the Party file will run and you will get output as
"withiout main".
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
4
posted
0
Abimaran Kugathasan wrote:Check, there are some other coding should be in the book, try find out it. Because, this is not complete!
I have only just recognised this example. It's from Head First Java, isn't it? That example is notorious for being incomplete; it cannot be run.
Virendrasinh Gohil
Ranch Hand
Joined: Jun 09, 2004
Posts: 46
posted
0
Campbell Ritchie wrote:I have only just recognised this example. It's from Head First Java, isn't it? That example is notorious for being incomplete; it cannot be run.
I agree. In fact, possibility is, it is not even meant for running. Head First use lot of code snippets for explaining the concepts. Those are not meant to be executed as is. They do have complete code base which executes as is and they mention it in book as and when it appears.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32694
4
posted
0
Virendrasinh Gohil wrote: . . . not even meant for running. . . .
Yes, you're right there.
sharma pankaj
Greenhorn
Joined: Jul 21, 2010
Posts: 6
posted
0
@ shanky
please see the attachement..
this program aint working for me..
Cody Long
Ranch Hand
Joined: Jan 01, 2009
Posts: 95
posted
0
Frank Thuring wrote:Please help me
I compile with the command promp in windows the next code:
import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite () {
Frame f = new Frame();
Label I = new Label ("Party at Tim's");
Button b = new Button ("You bet");
Button c = new Button ("Shoot me");
Panel p = new Panel();
p.add(I);
}
}
The compiler has made a class file Party. But when I give the command java Party then I got the error:
Exeption in thread "main" java.lang.NoSuchMethodError: main
Does anayone know how I mus solve this problem?
If you read the page that this example is on you will see that it specifically says that this code is not for compiling. They are just using it as an example. I tried to do the same thing but then i looked more closely.
Duct tape is like the Force. It has a dark side, a light side, and it binds the whole universe together.
go to toolbar.
click on run.
then add the name of the main as your class name add this to java application
select the jdk over there and then click on Run.
Your program should run.
As i have never seen the netbean but this process happens in eclipse.
can anybody help who knows netbeans.please help him in running the file in Netbeans.
I compile with the command promp in windows the next code:
import java.awt.*;
import java.awt.event.*;
class Party {
public void buildInvite () {
Frame f = new Frame();
Label I = new Label ("Party at Tim's");
Button b = new Button ("You bet");
Button c = new Button ("Shoot me");
Panel p = new Panel();
p.add(I);
}
}
The compiler has made a class file Party. But when I give the command java Party then I got the error:
Exeption in thread "main" java.lang.NoSuchMethodError: main
Does anayone know how I mus solve this problem?
If you read the page that this example is on you will see that it specifically says that this code is not for compiling. They are just using it as an example. I tried to do the same thing but then i looked more closely.
Yes this code is not meant for compiling but author of books always expect the reader to do some sort of self exercise
Dimitrios Chatzidimitriou
Greenhorn
Joined: Aug 21, 2010
Posts: 14
posted
0
Hello everybody! That is my first post and is nice to be here(I hope I won't regret the day I decided to learn java..)!
Anyway I am also reading the Head First Java book.. and I have the same problem but in a really first example..
The code is like this..
I just copied it from the book.. Do you think is incomplete? The book gave me the impression that this code should work while it has the result that is supposed to be..
bang bang ba-bang
ding ding da-ding
If the code is complete and should work fine.. then is disappointing that I read one page of the book and after that I need to spend hours searching the net on how to solve this kind of problems.
I may need to mention that I have windows 7 64bit and and I use DrJava as editor..
Welcome to javaRanch.
Please use CODETAGS while posting your post also start a new topic while posting new Question.
Yes ofcourse you will not regret for that.
If you have any problem in java you can post the same here on javaranch experts are always there to help you.
execution starts with a main method.
and main method is contained in that file so name of file is important
Dimitrios Chatzidimitriou
Greenhorn
Joined: Aug 21, 2010
Posts: 14
posted
0
Ok I somehow understand!
It is just strange because in other programming languages(i.e. vb) if you create a simple .exe file it does not matter how you call it.. just double click and will run!
Thanks for your fast replies, we almost do a live chat!
Avishkar Nikale
Ranch Hand
Joined: Aug 06, 2010
Posts: 173
posted
0
Dimitrios,
There is a concept of executable jar in Java where in you package your program/application into a archive
which can be double click & it too will run.
Dimitrios Chatzidimitriou wrote:Ok I somehow understand!
It is just strange because in other programming languages(i.e. vb) if you create a simple .exe file it does not matter how you call it.. just double click and will run!
Thanks for your fast replies, we almost do a live chat!
Welcome Sir.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Error: Exeption in thread "main" java.lang.NoSuchMethodError: main