eclipse中使用package导致applet嵌入html文件不可运行的问题

来源:互联网 发布:笔迹分析软件 编辑:程序博客网 时间:2024/05/21 17:51

  问题就是用eclipse写了个applet,用了package,然后创建一个html文件,嵌入applet后无法运行,如果不用package,而直接把程序拷到default package的文件中却可以。
在网上搜了下,解决方案如下:
http://topic.csdn.net/u/20080913/13/2d33fec4-c4a3-4349-b3d7-06fadce7d024.html
忽略了package的后果
源文件helloapplet.java因为在package applet中,所以在源文件第一行会有这样一行东西:
package applet;
......
在eclipse里运行小应用程序没有问题,但是用ie浏览器运行html文件,或者用appletviewer命令运行html文件,都会抛出class not found的异常。
helloapplet.html代码如下:
 

Code:
  1. <html>    
  2. <head>    
  3. <title>Hello Applet! </title>    
  4. </head>    
  5. <body>    
  6. <applet code="helloapplet.class" width=300 height=100>    
  7. </applet>    
  8. </body>    
  9. </html>    

在网上查了下,修改如下:
 

Code:
  1. <applet code="helloapplet.class" codebase="D:/eclipseworkspace/helloworld/bin/applet" width=300 height=100>    
  2. </applet>    

codebase指出class文件所在的基地目录,但是还是报相同的错误,折腾了很久,后来光华版友指出,错误和源文件第一行package语句有关。
于是想到引用包内定义类的格式应该是applet.helloapplet,那么对应的二进制文件引用方法也应该改为applet.helloapplet.class。于是做出
如下修改:
 

Code:
  1. <applet code="applet.helloapplet.class" codebase="D:/eclipseworkspace/helloworld/bin" width=300 height=100>    
  2. </applet>    

如此就ok了~:)
网上的那些入门applet教程都没有涉及到这些细节,所以想写一下,让后来的人少走些弯路。

文章出处:http://hi.baidu.com/whuwinnie/blog/item/c5050dd3aa899fd5a8ec9aea.html