教程上的代码
===================
import java.util.*;
import java.awt.*;
public class MyThread6_1_2 extends java.applet.Applet implements Runnable{
Thread clockThread;
public void start(){
System.out.println("start! ");
if(clockThread == null){
clockThread = new Thread( this, "Clock");
clockThread.start();
}
}
public void run(){
System.out.println("start! ");
while(clockThread != null){
repaint();
try{
clockThread.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
System.out.println("start! ");
Date now = new Date();
//g.drawString( now.getHours() + ":" now.getMinutes() + ":" + now.getSeconds(),5,10);
g.drawString("now.getSeconds()",5,10);
}
public void stop(){
System.out.println("start! ");
clockThread.stop();
clockThread = null;
}
}
===================
用 java MyThread6_1_2 运行时的Error 提示
=====================
C:\javaTest>java Mythread6_1_2
Exception in thread "main" java.lang.NoClassDefFoundError: Mythread6_1_2 (wrong
name: MyThread6_1_2)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
=====================
------解决思路----------------------
先javac Mythread6_1_2
------解决思路----------------------
先javac Mythread6_1_2 .java
------解决思路----------------------
楼上正解,先用javac编译成.class文件,然后在.class文件下java 如果有package 则在package的父文件夹下使用java 包名.类名
------解决思路----------------------
在确认下是否在同一路径下,代码没问题!
------解决思路----------------------
不是说你代码不对,首先你这个程序是applet,然后如果点class已经生成,那么运行命令应该是appletviewer Mythread6_1_2
而不是java Mythread6_1_2,不知道说的还明白