aspose file tools*
The moose likes Java in General and the fly likes nosuchmethoderror Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "nosuchmethoderror" Watch "nosuchmethoderror" New topic
Author

nosuchmethoderror

Alex Dolman
Greenhorn

Joined: Aug 09, 2005
Posts: 5
java.lang.NoSuchMethodError: main
Exception in thread "main"

Im creating a program that reads and writes data into and from text fields, and the program compiles, then i get this error. What exactly does it mean and how can i fix it? i can post my program if required. cheers
Tiago Fernandez
Ranch Hand

Joined: May 16, 2003
Posts: 167
Probably your output is messed up, so clean it and try to build again. Also you might be relying at a JAR file for compiling and using a different version at runtime.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/NoSuchMethodError.html


Tiago Fernandez
http://www.tiago182.spyw.com/
Tony William
Ranch Hand

Joined: Jun 27, 2005
Posts: 54
I am new to this forum.

In case where I have Java problem that would like to seek other's help, in what way should I let other people know exactly what problem I have? Can I somehow upload the java files?


MCP, MCP+I, MCSE(NT4), MCSE+I, MCSE(2000), MCDBA, MCSD(VS6)<br />SCJP 5.0, SCBCD 1.3<br />ICED(v5.0), ICSD (WSP5.0)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You can cut and paste code into a message; on the editing screen, look for the little "CODE" button, and put the code in between the "CODE" tags that it creates.

In any case, to execute a Java class an an application, it must have a main() method declared like this:

public static void main(String[] args) {

The only thing on that line that can change is the word "args" -- you're free to use any name you like. If your declaration for main() doesn't match the above, then you're going to get the error message you've indicated.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[EFH]: The only thing on that line that can change is the word "args"

Well, there are some other possibilities. In particular under JDK 5.0 you may now use varargs instead:

More trivially, you could also reverse the order of the "public" and "static" (or even add "synchronized" or "strictfp", and of course whitespace may be different. But those would be petty of me to mention.


"I'm not back." - Bill Harding, Twister
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Blast you, you alien super-genius Jim!

I was going to add, in parentheses, "(modulo trivial changes like ordering of the modifiers)," but decided to instead make a little white lie to give our new friend Alex a break.

To be honest, the varargs version didn't occur to me!
Alex Dolman
Greenhorn

Joined: Aug 09, 2005
Posts: 5
Right well! breaks is exactly what i need ha. So where exactly do i need to add this "public static void main(String[] args) {" into my code?
My prgram consists of a Form and a class, and they both compile fine but im still getting the error
java.lang.NoSuchMethodError: main
Exception in thread "main"
Do i have to add it to both the class and the form or what?
Many thanks Alex
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
The java program when it loads and starts the execution of the a class, looks for a specific method to give control to. That method is the one everyone is talking about: main Defined as: public static void main(String[] args);
You can code it anywhere in the class that you'll reference on the java.exe command line.
The java command needs to know where to start, and this is the convention that it uses. If you have a Java program from which you want to start another java program you would code something like:
ItsClassName.main(new String[] {arg1, arg2});
to get the it started.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You can (and should) declare args final also
...and since it's a public API, you have to handle the case of null - the most unfortunate of language flaws. The optimal workaround to the existence of null is failing as early as possible, arguably with a java.lang.NullPointerException.
The idiom that I use (since I implement the J2SE 5.0 spec. as my day job) is:


[ August 13, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you haven't fixed this problem yet, please describe what you do to compile and run your program. If you are using the command line, include the exact commands you type and the output produced. This will help us be able to answer your questions more precisely.

Thanks,

Layne


Java API Documentation
The Java Tutorial
Alex Dolman
Greenhorn

Joined: Aug 09, 2005
Posts: 5
It doesnt sound like threres a lot wrong but im just having problems getting it right! Can i send somebody the whole thing to look at as i really am having trouble understanding these explanations? Thanks all
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
It sounds like the problem could be what you type in, not's what's in the program.

If the problem is when you execute the java.exe command, open a Dos window/Command prompt and enter the command you've been entering and then copy the contents of the screen and post it here.

Have you ever gotten a java program to execute? For example the usual simple HelloWorld program? If you have, look at the main() method in that program for how it should be coded:
Replace <Program-name> with the name of class.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Alex Dolman:
java.lang.NoSuchMethodError: main
Exception in thread "main"

Im creating a program that reads and writes data into and from text fields, and the program compiles, then i get this error. What exactly does it mean and how can i fix it?


I kinda get the feeling this thread should be moved into Java in General (beginner).

After you compile your program, Alex, how do you try to run it? Do you type
java MyProgram
at a DOS prompt?
[ August 14, 2005: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Alex Dolman
Greenhorn

Joined: Aug 09, 2005
Posts: 5
Im using netbeans IDE 3.6, that make anything clearer?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Is your form an html form, an applet, or a GUI (frame)?

I've never used netbeans IDE, but I'm guessing that you go to the menu and hit "run" to try to make it work. Am I close?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Alex Dolman:
Im using netbeans IDE 3.6, that make anything clearer?


That helps somewhat. I'm familiar with NetBeans, but it's been a while since I used version 3.6. The changes in version 4.0 are fairly drastic. Can you PLEASE describe what you do to run your program? I know I'm repeating myself, but this will help decrease the amount of guess-work we have to do in order to answer your question.

Layne
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi, Alex -

1. Basic problem:
As I'm sure you either knew or have probably guessed by now:

a) Everything in Java is organized into "classes"

b) A Java program can have one class, or many

c) At least one of these classes needs to have a "main method":
public static void main (String[] args)

d) The problem, "java.lang.NoSuchMethodError: main", means either
that that you don't have any such method "main()", or that
the Java runtime can't find it.

2. Resolving the problem
How to go about troubleshooting the problem depends greatly on
how you're trying to compile and run the program.

In your case, you're using NetBeans.

I'm currently using NetBeans 4.1 (which Sun provides free, bundled
with Java 5.0. The only "gotcha" is that it's a 100++MB download).

I would encourage you to upgrade to 4.1, but I can give you an
example using NetBeans 3.5.1 (you have 3.6). Let's create a
minimal, so-called "hello world" program.

3. Java/NetBeans "Hello World":
a) Start NetBeans. Let's assume Java is configured correctly.

b) Select:
File | New | Templates | Java Classes | Java Main Class,
[Next]
Name= Hello (that's capital "H"),
[Finish]

c) The editor should come up. Add the following line to the template:

d) Go into Netbean's "Explorer" pane (to the left)
<Right Click>Hello, Select "Build"

Hopefully, "Hello" will compile without error.

e) <Right Click>Hello (again), Select "Execute"

Hopefully, you'll see "Hello from Java!" printed in the
output window at the bottom.

4. With any tool: from an IDE (like NetBeans, Eclipse, Idea4J, or
Jbuilder) it's usually very helpful to do this kind of "Hello World"
before trying anything else.

5. The Netbeans help comes with a "Getting Started" tutorial. I
believe this tutorial will help you with exactly what you need
to get going with the GUI you're trying to build.

I hoped that helps (at least a bit ;-)

And Good luck!

Sincerely .. PSM
[ August 14, 2005: Message edited by: Paul Santa Maria ]

Paul M. Santa Maria, SCJP
ryan zhang
Greenhorn

Joined: Aug 09, 2005
Posts: 3
Originally posted by Alex Dolman:
It doesnt sound like threres a lot wrong but im just having problems getting it right! Can i send somebody the whole thing to look at as i really am having trouble understanding these explanations? Thanks all

hi,Alex,is your problem clear,if not,i'd like to help u.u can send me email : zhangcheng@ufgov.com.cn or msn :ryan.zhangcheng@gmail.com
Alex Dolman
Greenhorn

Joined: Aug 09, 2005
Posts: 5
Yes its just a matter of clicking the compile function. I'm going to send my work to ryan and see if he can explain the problem any better for you guys and me! cheers
 
 
subject: nosuchmethoderror