Question

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

I am fairly new to Java, and I am unable to figure out why I am getting NoSuchMethodError: main when I execute the following code. I am not sure what does the NoSuchMethodError is pertaining to. It looks like I have everything right. Please help me out here. Thanks a lot.

public class ThreadExample1 extends Thread 
 {
    static String[] msg = {"Java", "programming", "is", "the", "best"};
    public ThreadExample1(String id) 
    {
       super(id);
     }
    @Override
    public void run() 
        {
         try 
           {
             Output.displayList(getName(), msg);
            } 
    catch (InterruptedException ex) 
        {

        }
    }
  }

class Output 
 {
  public static void displayList(String name, String list[]) throws InterruptedException 
   {
     for (int i = 0; i < list.length; i++) 
    {
          Thread.currentThread().sleep((long) (3000 * Math.random()));
          System.out.println(name + list[i]);
         }
    }
   public static void main(String[] args) 
    {
         ThreadExample1 thread1 = new ThreadExample1("thread1: ");
         ThreadExample1 thread2 = new ThreadExample1("thread2: ");
         thread1.start();
         thread2.start();
         boolean t1IsAlive = true;
         boolean t2IsAlive = true;
         do 
          {
          if (t1IsAlive && !thread1.isAlive()) 
            {
              t1IsAlive = false;
              System.out.println("t1 is dead.");
             }
          if (t2IsAlive && !thread2.isAlive()) 
            {
              t2IsAlive = false;
              System.out.println("t2 is dead.");
              }
           }while (t1IsAlive || t2IsAlive);
     }
}
Was it helpful?

Solution

I don't have any problem compiling and executing the above code ... Keep in mind that when you want to execute it , you need to use this command line :

java Output

and NOT :

java ThreadExample1

because the main method is within the Output calss and not in ThreadExample1 ...

OTHER TIPS

Save the file as ThreadExample1.java and compile. After that you should run Output class but not the ThreadExample1 class. This is because you have added your main method inside Output class. But since you have made your ThreadExample1.java class public you have to save and compile using that name(javac ThreadExample1.java). After that java Output

Take a look at code-snippet the main() method is in Output class.

Use following command line to launch the Output.main() method:

c:\>java Output

When you do compile a java program you do need to give the file name after javac.

like javac MyProgram.java

and when you do run it using java then you need to mention the name of the class that is having "public static void main(String args[])" method.

Say I have two classes in MyProgram.java : Class First and Class Second

and I have "public static void main(String args[])" in Class Second then I will do the following :

javac MyProgram.java
java Second
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top