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);
}
}
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 ...