【发布时间】:2023-03-11 19:17:02
【问题描述】:
我正在学习 Youtube 上的 Java 教程中的 Applet。当我使用 IntelliJ 编译代码时,一切都很顺利。 Applet Viewer 也能正常工作。
但是当我在 html 文件中插入小程序代码(我将其命名为 index.html),并且我将 HelloWorld.class 复制到桌面(其中包含主要内容)然后我打开了 index.html 然后在浏览器中发现只是空白。
这是我的HelloWorld.java & index.html
HelloWorld.java
package com.example.helloworld;
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet{
public void paint(Graphics g){
super.paint(g);
g.drawString("wow ",25, 25);
}
}
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<applet code="/Users/Peterhon/Desktop/HelloWorld.class" width="400" height="100"></applet>
</body>
</html>
当我运行applet viewer index.html时,我得到了如下所示的错误
java.lang.NoClassDefFoundError: /Users/Peterhon/Desktop/HelloWorld (wrong name: com/example/helloworld/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:217)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:152)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.applet.AppletClassLoader.loadCode(AppletClassLoader.java:626)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:804)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:733)
at sun.applet.AppletPanel.run(AppletPanel.java:378)
at java.lang.Thread.run(Thread.java:745)
谁能给我一个提示?
【问题讨论】:
-
给出你电脑中的小程序类的实际路径,例如
<applet code="c:/HelloWorld.class" width="400" height="100"></applet> -
@JBALI 我已经尝试过这样做,但仍然遇到上面已更新的类似错误。我希望你会看到:-)
-
这里的code属性
<applet code="/Users/Peterhon/Desktop/HelloWorld.class" width="400" height="100"></applet>应该是类的全限定名,所以<applet code="com.example.helloworld.HelloWorld" width="400" height="100"></applet>.. -
@AndrewThompson 你什么意思????
-
你不明白什么? (请注意,添加四个“?”根本不会帮助清晰。)
标签: java html intellij-idea applet noclassdeffounderror