/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.applet;import java.applet.*; // Don't forget this import statement!
import java.awt.*;
// Or this one for the graphics!import java.util.Date; // To obtain the current time
import java.text.DateFormat;
// For displaying the time/**
* This applet displays the time, and updates it every second
**/public class Clock extends Applet implements Runnable {
Label time; // A component to display the time in
DateFormat timeFormat; // This object converts the time to a string
Thread timer; // The thread that updates the time
volatile boolean running; // A flag used to stop the thread /**
* The init method is called when the browser first starts the applet.
* It sets up the Label component and obtains a DateFormat object
**/
public void init() {
time = new Label();
time.setFont(new Font("helvetica", Font.BOLD, 12));
time.setAlignment(Label.CENTER);
setLayout(new BorderLayout()); add(time, BorderLayout.CENTER);
timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
} /**
* This browser calls this method to tell the applet to start running.
* Here, we create and start a thread that will update the time each
* second. Note that we take care never to have more than one thread
**/
public void start() {
running = true; // Set the flag
if (timer == null) { // If we don't already have a thread
timer = new Thread(this); // Then create one
timer.start(); // And start it running
}
} /**
* This method implements Runnable. It is the body of the thread. Once
* a second, it updates the text of the Label to display the current time
**/
public void run() {
while(running) { // Loop until we're stopped
// Get current time, convert to a String, and display in the Label
time.setText(timeFormat.format(new Date()));
// Now wait 1000 milliseconds
try { Thread.sleep(1000); }
catch (InterruptedException e) {}
}
// If the thread exits, set it to null so we can create a new one
// if start() is called again.
timer = null;
} /**
* The browser calls this method to tell the applet that it is not visible
* and should not run. It sets a flag that tells the run() method to exit
**/
public void stop() { running = false; } /**
* Returns information about the applet for display by the applet viewer
**/
public String getAppletInfo() {
return "Clock applet Copyright (c) 2000 by David Flanagan";
}}
程序是copy下来的,在C:\java 保存为.java 文件,编译生成.class文件在C:\java中存盘,编译可以通过,第一次直接运行输入命令行 java Clock显示:
Exception in thread "main" java.lang.NoclassDefoundError:Clock<wrong name: com/davidflanagan/examples/applet/Clock>
at java.lang.ClassLoader.defineClass1<Native Method>
at java.lang.ClassLoader.defineClass1<Unknown Source>
at java.security.secureClassLoader.defineClass1<Unknown Source>
.
.
.
在命令提示符后输入set CLASSPATH=.:C:\java后在运行java Clock
运行显示:
Exception in thread "main" java.lang.NoclassDefoundError:Clock
哪位大虾帮忙看看,不胜感激!
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.com/javaexamples2.
*/
package com.davidflanagan.examples.applet;import java.applet.*; // Don't forget this import statement!
import java.awt.*;
// Or this one for the graphics!import java.util.Date; // To obtain the current time
import java.text.DateFormat;
// For displaying the time/**
* This applet displays the time, and updates it every second
**/public class Clock extends Applet implements Runnable {
Label time; // A component to display the time in
DateFormat timeFormat; // This object converts the time to a string
Thread timer; // The thread that updates the time
volatile boolean running; // A flag used to stop the thread /**
* The init method is called when the browser first starts the applet.
* It sets up the Label component and obtains a DateFormat object
**/
public void init() {
time = new Label();
time.setFont(new Font("helvetica", Font.BOLD, 12));
time.setAlignment(Label.CENTER);
setLayout(new BorderLayout()); add(time, BorderLayout.CENTER);
timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM);
} /**
* This browser calls this method to tell the applet to start running.
* Here, we create and start a thread that will update the time each
* second. Note that we take care never to have more than one thread
**/
public void start() {
running = true; // Set the flag
if (timer == null) { // If we don't already have a thread
timer = new Thread(this); // Then create one
timer.start(); // And start it running
}
} /**
* This method implements Runnable. It is the body of the thread. Once
* a second, it updates the text of the Label to display the current time
**/
public void run() {
while(running) { // Loop until we're stopped
// Get current time, convert to a String, and display in the Label
time.setText(timeFormat.format(new Date()));
// Now wait 1000 milliseconds
try { Thread.sleep(1000); }
catch (InterruptedException e) {}
}
// If the thread exits, set it to null so we can create a new one
// if start() is called again.
timer = null;
} /**
* The browser calls this method to tell the applet that it is not visible
* and should not run. It sets a flag that tells the run() method to exit
**/
public void stop() { running = false; } /**
* Returns information about the applet for display by the applet viewer
**/
public String getAppletInfo() {
return "Clock applet Copyright (c) 2000 by David Flanagan";
}}
程序是copy下来的,在C:\java 保存为.java 文件,编译生成.class文件在C:\java中存盘,编译可以通过,第一次直接运行输入命令行 java Clock显示:
Exception in thread "main" java.lang.NoclassDefoundError:Clock<wrong name: com/davidflanagan/examples/applet/Clock>
at java.lang.ClassLoader.defineClass1<Native Method>
at java.lang.ClassLoader.defineClass1<Unknown Source>
at java.security.secureClassLoader.defineClass1<Unknown Source>
.
.
.
在命令提示符后输入set CLASSPATH=.:C:\java后在运行java Clock
运行显示:
Exception in thread "main" java.lang.NoclassDefoundError:Clock
哪位大虾帮忙看看,不胜感激!
你把类保存到F:盘跟目录下
set classpath=f:\
javac com.davidflanagan.examples.applet.Clock.java
package com.davidflanagan.examples.applet;
去掉package com.davidflanagan.examples.applet;
编译后运行结果:
Exception in thread "main" java.lang.NoclassDefoundError:mainwhy?
按照你的方法也执行不下去,输出:
错误:无法读取:com.davidflanagan.examples.applet.Clock.java
1错误
2,applet程序;
3,没有main函数。
你还想怎么折腾你的jvm?