aspose file tools*
The moose likes Beginning Java and the fly likes Error: Exeption in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error: Exeption in thread "main" java.lang.NoSuchMethodError: main" Watch "Error: Exeption in thread "main" java.lang.NoSuchMethodError: main" New topic
Author

Error: Exeption in thread "main" java.lang.NoSuchMethodError: main

Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
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?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Do you have a main() method here? So how can you run the program? What is the entry point of your program without main() method?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Frank Thuring
Greenhorn

Joined: Aug 10, 2010
Posts: 28
I don't have a main method. This is the whole pogram.

It is a excercise in a book head first Java. Is the exercise wrong and must I always have a main in every program?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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
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
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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.....


SCJP6.0,My blog Ranchers from Delhi
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Welcome to JavaRanch.

Please UseCodeTags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Campbell Ritchie wrote:
shanky sohar wrote: . . . we can also run the program sucessfully without a main method . . .
. . . but only as a party trick



Yes.......you are right......
sharma pankaj
Greenhorn

Joined: Jul 21, 2010
Posts: 6
@ shanky

class Party {
static{
System.out.println("withiout main");
System.exit(0);
}
}

this is not working for me..




Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

pankaj sharma newbie wrote:@ shanky

class Party {
static{
System.out.println("withiout main");
System.exit(0);
}
}

this is not working for me..






mention the error which you got
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051




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: 39436
    
  28
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
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: 39436
    
  28
Virendrasinh Gohil wrote: . . . not even meant for running. . . .
Yes, you're right there.
sharma pankaj
Greenhorn

Joined: Jul 21, 2010
Posts: 6
@ shanky

please see the attachement..

this program aint working for me..



[Thumbnail for untitled.JPG]

Cody Long
Ranch Hand

Joined: Jan 01, 2009
Posts: 95
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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

in netbeans.

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Cody Long wrote:
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.

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
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..

Thank you for your time!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@Dimitrios Chatzidimitriou

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.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

YEs the Question is complete

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


i think you are saving your code as DrumKit
try saving it with DrumKitTestDrive and then run it
Dimitrios Chatzidimitriou
Greenhorn

Joined: Aug 21, 2010
Posts: 14
shanky sohar wrote:
i think you are saving your code as DrumKit
try saving it with DrumKitTestDrive and then run it


It ran!

The thing is that I am sure I tried java DrumKitTestDrive before!

Anyway I am happy that it was my fault!

Thank you very very much!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Dimitrios Chatzidimitriou wrote:
Thank you very very much!


Welcome sir.
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
shanky sohar wrote:
i think you are saving your code as DrumKit
try saving it with DrumKitTestDrive and then run it


Nice catch shanky !


Regards,
Avishkar Nikale
Dimitrios Chatzidimitriou
Greenhorn

Joined: Aug 21, 2010
Posts: 14
Can you explain why is the name of the file so important?

Thank you!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Dimitrios Chatzidimitriou wrote:Can you explain why is the name of the file so important?

Thank you!

it is as simple as saying why your name is so important.okay tell me if i call you by any name did you listen to me
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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
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
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.

More as you dive deeper.

I assure you the journey is worth it.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

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://aspose.com/file-tools
 
subject: Error: Exeption in thread "main" java.lang.NoSuchMethodError: main