浏览器打开含有applet标签网页报ClassNotFoundException错误的解决办法
----------------------------------~~~~~~~~~~~~~~~上班太无聊,闲的我开始重新学JAVA~~~~~~~~~~~--------------------------------------------------
先贴上applet代码:气泡上升示例
package applet;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import javax.management.loading.PrivateClassLoader;
@SuppressWarnings("serial")
public class applet extends Applet implements Runnable {
Thread artisThread=null;
int bubble=0,thisbubble=0;
int MAXBUBBLIES=20;
int stepper=4;
int record[][]=new int [MAXBUBBLIES][7];
public void init(){
resize(400,400);
}
public void draw_bubble(int thisbubble,Graphics g)
{
record[thisbubble][0]=(int)(Math.random()*400);
record[thisbubble][1]=400;
record[thisbubble][2]=(int)(Math.random()*100)/2;
record[thisbubble][3]=(int)(Math.random()*255);
record[thisbubble][4]=(int)(Math.random()*255);
record[thisbubble][5]=(int)(Math.random()*8);
record[thisbubble][6]=(int)(Math.random()*10);
Color col= new Color(record[thisbubble][3], record[thisbubble][4],255);
g.setColor(col);
g.fillOval(record[thisbubble][0], record[thisbubble][1], record[thisbubble][2],record[ thisbubble][2]);
}
public void move_bubble(int x,int y,int r,Color col,int step,int step2,Graphics g)
{
g.setColor(Color.white);
g.fillOval(x, y, r, r);
g.setColor(col);
g.fillOval(x-step, y-step2, r, r);
}
public void paint(Graphics g)
{
int i,j,tmp;
if(bubble<MAXBUBBLIES || thisbubble<MAXBUBBLIES)
{
draw_bubble( thisbubble, g);
if(bubble< MAXBUBBLIES)
{
bubble++;
thisbubble++;
}
else
thisbubble=MAXBUBBLIES;
}
for (i=0;i<bubble;++i)
{
if(i%5<=stepper)
{
move_bubble( record[i][0], record[i][1], record[i][2],
new Color(record[i][3], record[i][4],255),record[i][5],record[i][6],g);
record[i][0]-=record[i][5];
record[i][1]-=record[i][6];
if(record[i][0]+2* record[i][2]<0||record[i][1]+2* record[i][2]<0)
{
draw_bubble(i, g);
}
}
}
}
public void update (Graphics g)
{
paint(g);
g.drawImage(null,100,100,null);
}
public void start()
{
if(artisThread==null )
{
artisThread=new Thread(this);
artisThread.start();
}
}
public void stop()
{
artisThread=null;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
repaint();
try {
artisThread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
效果如下
在applet.class文件所在目录放入html文件
<html>
<head>
<title>AnApplet</title>
</head>
<body>
<applet code = "applet.class" width = 400 height = 400>
</applet>
</body>
</html>
结果使用IE浏览器打开的时候报ClassNotFoundException错误,经过摸索,解决方法有两个:
一、code改为包.类名.class,同时把html文件放到和包一个目录下,增加包的相对路径,code_base,html文件更改如下:
<html>
<head>
<title>AnApplet</title>
</head>
<body>
<applet code_base="applet/" code = "applet.applet.class" width = 400 height = 400>
</applet>
</body>
</html>
二、把包改成默认包,同时去掉package applet,重新生成class文件,把html文件放入和class文件同一目录下,html内容如下:
<html>
<head>
<title>AnApplet</title>
</head>
<body>
<applet code = "applet.class" code_base="/" width = 400 height = 400>
</applet>
</body>
</html>
经验证,两个方法都可以解决ClassNotFoundException的问题。