Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frank Thuring
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check, there are some other coding should be in the book, try find out it. Because, this is not complete!
 
Tim Batts
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please UseCodeTags when you post source code.
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote: . . . we can also run the program sucessfully without a main method . . .
. . . but only as a party trick
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ shanky

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

this is not working for me..




 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Virendrasinh Gohil wrote: . . . not even meant for running. . . .
Yes, you're right there.
 
sharma pankaj
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ shanky

please see the attachement..

this program aint working for me..
untitled.JPG
Error
[Thumbnail for untitled.JPG]
 
Cody Long
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YEs the Question is complete

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i think you are saving your code as DrumKit
try saving it with DrumKitTestDrive and then run it
 
Dimitrios Chatzidimitriou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimitrios Chatzidimitriou wrote:
Thank you very very much!


Welcome sir.
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shanky sohar wrote:
i think you are saving your code as DrumKit
try saving it with DrumKitTestDrive and then run it


Nice catch shanky !
 
Dimitrios Chatzidimitriou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain why is the name of the file so important?

Thank you!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
execution starts with a main method.
and main method is contained in that file so name of file is important
 
Dimitrios Chatzidimitriou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic