这是从书中抄录的程序,编译通过,但运行时出现下列错误:
java.lang.NoSuchMethodError: main
Exception in thread "main"我百思不得其解,程序代码如下,请高手指点:// 直接扩展Thread类,创建和运行线程的测试
class TestedThread extends Thread
{
private int countDown = 4;
private int threadNumber;
private static int threadCount = 0; public TestedThread()
{
threadNumber = ++threadCount;
System.out.println("在构造方法体内:(" + threadNumber + ") 创建线程" + getName());
} public void run()
{
while (true)
{
System.out.println("在run()方法体内:(" + threadNumber + ") " + getName());
if (--countDown == 0)
return;
}
}
}public class ThreadTest
{
public static void main(String args[])
{
for(int i = 0; i < 4; i++)
{
new TestedThread().start();
}
System.out.println("\n *** 所有的线程都已启动! *** \n");
}
}
java.lang.NoSuchMethodError: main
Exception in thread "main"我百思不得其解,程序代码如下,请高手指点:// 直接扩展Thread类,创建和运行线程的测试
class TestedThread extends Thread
{
private int countDown = 4;
private int threadNumber;
private static int threadCount = 0; public TestedThread()
{
threadNumber = ++threadCount;
System.out.println("在构造方法体内:(" + threadNumber + ") 创建线程" + getName());
} public void run()
{
while (true)
{
System.out.println("在run()方法体内:(" + threadNumber + ") " + getName());
if (--countDown == 0)
return;
}
}
}public class ThreadTest
{
public static void main(String args[])
{
for(int i = 0; i < 4; i++)
{
new TestedThread().start();
}
System.out.println("\n *** 所有的线程都已启动! *** \n");
}
}
结果:在构造方法体内:(1) 创建线程Thread-0
在构造方法体内:(2) 创建线程Thread-1
在构造方法体内:(3) 创建线程Thread-2
在构造方法体内:(4) 创建线程Thread-3 *** 所有的线程都已启动! ***在run()方法体内:(1) Thread-0
在run()方法体内:(2) Thread-1
在run()方法体内:(3) Thread-2
在run()方法体内:(4) Thread-3
在run()方法体内:(1) Thread-0
在run()方法体内:(2) Thread-1
在run()方法体内:(3) Thread-2
在run()方法体内:(4) Thread-3
在run()方法体内:(1) Thread-0
在run()方法体内:(2) Thread-1
在run()方法体内:(3) Thread-2
在run()方法体内:(4) Thread-3
在run()方法体内:(1) Thread-0
在run()方法体内:(2) Thread-1
在run()方法体内:(3) Thread-2
在run()方法体内:(4) Thread-3
Press any key to continue...
Run ThreadTest.java using Untitled Runtime Configuration 的时候是直接以你的ThreadTest.java作为主文件类似于
javac ThreadTest.java
java ThreadTest而你用run project的时候,会跑project下面的主文件,如果你的project没有设置主文件的时候,或者没有把ThreadTest.java设置成主文件的时候,jbuilder会找不到main在哪里。
感谢各位的参与。揭帖。