File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception 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 "Exception in thread "main" java.lang.NoSuchMethodError: main" Watch "Exception in thread "main" java.lang.NoSuchMethodError: main" New topic
Author

Exception in thread "main" java.lang.NoSuchMethodError: main

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Any idea why this program is not running?



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


03/07/2011 21:42 <DIR> .
03/07/2011 21:42 <DIR> ..
03/07/2011 21:40 640 SimpleThread.class
03/07/2011 21:42 404 SimpleThread.java
03/07/2011 21:41 521 SimpleThreadTest.class
03/07/2011 21:41 302 SimpleThreadTest.java
4 File(s) 1,867 bytes
2 Dir(s) 37,588,905,984 bytes free

>javac SimpleThread
error: Class names, 'SimpleThread', are only accepted if annotation processing is explicitly requested
1 error

>javac SimpleThreadTest
error: Class names, 'SimpleThreadTest', are only accepted if annotation processing is explicitly requested
1 error

>

Somehow it runs in Eclipse, but the CLI and TextPad don't compile

Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

You must specify the filename with the javac command:

javac SimpleThreadTest.java

Not just the class name. (Did you pay attention to the error message that the compiler printed?).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

you also need to keep the name of public class and file name same, otherwise it won't compile.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
yes Javin. So will the following modifications will make the code work?

1. Have the file name as SimpleThreadTest.java
2. Change the SimpleThread class from public to default
3. Compile only SimpleThreadTest & run only SimpleThreadTest

Above changes i think should be made since main() method is present only in the SimpleThreadTest class. Correct me.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281

Absolutely correct , though I haven't run it , you are the step by step guide master (was reading another thread where you have provided step by step solution )
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

John Jai wrote:yes Javin. So will the following modifications will make the code work?

1. Have the file name as SimpleThreadTest.java
2. Change the SimpleThread class from public to default
3. Compile only SimpleThreadTest & run only SimpleThreadTest

Above changes i think should be made since main() method is present only in the SimpleThreadTest class. Correct me.


Yes excuse my typos, why does the class have to be default and cannot be public?
I was expecting the code within the run() method to run, however it does not, any idea why?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Because you did not start the thread. Just creating a Thread object is not enough, you have to call start on it.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesper de Jong wrote:Because you did not start the thread. Just creating a Thread object is not enough, you have to call start on it.


Well, I tried mimicking this tutorial, however, I still get the same error:

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

Carefully read and follow John Jai's advice, that will solve your problem, with one extra point: make class SimpleThreadTest public.

John Jai wrote:yes Javin. So will the following modifications will make the code work?

1. Have the file name as SimpleThreadTest.java
2. Change the SimpleThread class from public to default
3. Compile only SimpleThreadTest & run only SimpleThreadTest

Above changes i think should be made since main() method is present only in the SimpleThreadTest class. Correct me.


Please make an effort to understand why this will solve the problem.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Jesper de Jong wrote:Carefully read and follow John Jai's advice, that will solve your problem, with one extra point: make class SimpleThreadTest public.

John Jai wrote:yes Javin. So will the following modifications will make the code work?

1. Have the file name as SimpleThreadTest.java
2. Change the SimpleThread class from public to default
3. Compile only SimpleThreadTest & run only SimpleThreadTest

Above changes i think should be made since main() method is present only in the SimpleThreadTest class. Correct me.


Please make an effort to understand why this will solve the problem.


Sorry, indeed this works, the problem is that the compiler requires a class named Dog within a file named Dog.java, and, so on, which is a petty mistake



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NoSuchMethodError: main