Java 无法在html上运行JApplet,ClassNotFoundException
我写了一个简单的JApplet,我想在我的网站上运行它。我将其打包到一个.jar文件中,并在html上使用applet标记。这是我的html:Java 无法在html上运行JApplet,ClassNotFoundException,java,html,applet,classnotfoundexception,japplet,Java,Html,Applet,Classnotfoundexception,Japplet,我写了一个简单的JApplet,我想在我的网站上运行它。我将其打包到一个.jar文件中,并在html上使用applet标记。这是我的html: <html> <applet code="main.class" codebase="test/" archive="tmp.jar" width="600" height="95"> <param name="type" value="hello"> <param name="IP" value="127.0
<html>
<applet code="main.class"
codebase="test/"
archive="tmp.jar"
width="600" height="95">
<param name="type" value="hello">
<param name="IP" value="127.0.0.1">
</html>
但是当我打开web文件时,它捕获了ClassNotFoundException。请帮助我或给我一些建议。非常感谢。尝试编译
.java
文件以获取.class
文件,并将其粘贴到测试/
中。检查是否有错误。您是否检查了Java控制台的详细错误?我已经检查了,没有错误。您是否认为HTML和tmp.jar的目录结构和位置对解决此问题很重要?请将该信息包括在内。顺便说一句-1)g.drawImage(image,0,0,null)
应该是g.drawImage(image,0,0,this)因为每个JComponent
都是一个ImageObserver
。2) image=ImageIO.read(新文件(“img.jpg”)代码>小程序的资源需要加载为URL
,而不是文件
。优选地,相对于HTML中指定的codebase
或文档库形成的URL
。
public class main extends JApplet{
public String str, IP;
public ImagePanel panel;
protected void loadAppletParameters(){
String at = getParameter("type");
str = (at != null) ? at : "world";
at = getParameter("IP");
IP = (at != null) ? at : "127.0.0.1";
}
public void init(){
loadAppletParameters();
System.out.println("hi: ");
panel = new ImagePanel();
this.add(panel);
}
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File("img.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
public void change_image(String path){
try {
image = ImageIO.read(new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}