aspose file tools*
The moose likes Beginning Java and the fly likes main method problem 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 "main method problem" Watch "main method problem" New topic
Author

main method problem

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hey all I got a annoying problem, that I hope you guys can help out with..I have been learning java for a few months now, and all of a sudden, I have this strange main method problem that pops up on a few small applications I have made.
Exception in thread "main" java.lang.NoSuchMethodError: main
As far as I know, this error statement means my
public static void main(String args[]) method is written wrong.
Im really confused why this is happening, it doesnt happen to all my applications, only a few of them...I copied the code for a few of the applications to a friend and it worked fine on his machine....any help would be really appreciated
Marc
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Check the spelling of the class name inside the file. If you invoke "java Test" and in the class it is declared "java test" you will get that message. DOS will hand it in as the parameter ignoring the case. Java is not so forgiving.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the speedy reply...but that was one of things I thought of already....can you think of any other reason I would get that error??
Marc
suneeta prabhu
Greenhorn

Joined: Jun 03, 2001
Posts: 8
hi everyone,
even i have the same problem.
i compiled the foll code as test.java and tried to run it. got exception in thread "main" jav.lang.NoClassDefFoundError
can anyone please help?
class Monitor {
public synchronized void printOutMsg(String out) {
System.out.println(out);
System.out.println("Pausing thread...");
try {
wait();
} catch (InterruptedException ie) { }
System.out.println("Resuming thread...");
}
public synchronized void notifyThreads() {
System.out.println("Notifying all threads.");
notifyAll();
}
}

class test {
static Monitor m = new Monitor();
static class MsgPrinter extends Thread {
public void run() {
m.printOutMsg("BOO!");
System.out.println("End MsgPrinter run() method.");
}
}
static class Notifier implements Runnable {
public void run() {
try {
Thread.sleep (5000);
} catch (InterruptedException ie) { }
m.notifyThreads();
System.out.println("End Notifier run() method.");
}
}
public static void main(String args[]) {
MsgPrinter msgPrinter = new MsgPrinter();
msgPrinter.start();
Notifier notifier = new Notifier();
Thread notifierThread = new Thread(notifier);
notifierThread.start();
}
}

Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Suneeta,
Well, it tried it and it works fine. Boo to you too! Of course I can't SEE what you are actually typing when you try to RUN the code, but if you type
>java test
it runs fine. If you type
>java Test
you get that error message.
Jeffrey Schneider
Greenhorn

Joined: Jun 03, 2001
Posts: 1
I am having the same problem: Exception in thread "main" java.lang.NoSuchMethodError: main, with HelloWorld.
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet{
Label helloLabel = new Label ("Hi there");
public void init () {
setBackground (Color.green);
add (helloLabel);
}
}
Taken from Java Programming for Dummies, using JDK 1.2.1
I call java HelloWorld and get the message, BUT when I call the program through my test HTML program it works fine. What is going on? Thanks in advance.
[This message has been edited by Jeffrey Schneider (edited June 03, 2001).]
[This message has been edited by Jeffrey Schneider (edited June 03, 2001).]
KH Yau
Greenhorn

Joined: May 20, 2001
Posts: 13
Make sure you have set classpath correctly, or call codebase. Use visual age and u don't have to worry for the classpth.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
If it was a classpath problem the error would be something about not finding the class. In this case it found the class, it just can't find the main method in the class.
Jeffery,
An applet is NOT an application. You can not execute if from the command line in DOS. It doesn't HAVE a main method so amazingly enough the JVM can't find it. You can ONLY execute an applet in a browser or in an appletviewer. Not the same as the above problem.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
still need help...anyone?..I have double and triple checked all my spelling, and made sure everything was in the right dir. but for some reason, on a few programs I still get "main method not found"
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Just a possibility - is it possible that you have ANOTHER file with the same name in the classpath (different directory), and that the system is trying to execute that one?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the suggestion Cindy..I checked for files with the same name..did not find any
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Did you try to copy and paste the main method signature over from one that works? It is VAGUELY possible that you could have a corrupt bit of data on the line that is not visible, but is messing with the signature.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
god bless ya cindy! I figured out what was wrong...I use TextPad..and I recently made a type of "hot key" for the main method...so I decided to "hand" type it, and it worked fine!!!
This problem was really starting to bug me...Thanks a ton!
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
god bless ya cindy! I figured out what was wrong...I use TextPad..and I recently made a type of "hot key" for the main method...so I decided to "hand" type it, and it worked fine!!!
This problem was really starting to bug me...Thanks a ton!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: main method problem