【问题标题】:load: class com.example.helloworld.HelloWorld.class not found加载:未找到类 com.example.helloworld.HelloWorld.class
【发布时间】: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)

谁能给我一个提示?

【问题讨论】:

  • 给出你电脑中的小程序类的实际路径,例如&lt;applet code="c:/HelloWorld.class" width="400" height="100"&gt;&lt;/applet&gt;
  • @JBALI 我已经尝试过这样做,但仍然遇到上面已更新的类似错误。我希望你会看到:-)
  • 这里的code属性&lt;applet code="/Users/Peterhon/Desktop/HelloWorld.class" width="400" height="100"&gt;&lt;/applet&gt;应该是类的全限定名,所以&lt;applet code="com.example.helloworld.HelloWorld" width="400" height="100"&gt;&lt;/applet&gt;..
  • @AndrewThompson 你什么意思????
  • 你不明白什么? (请注意,添加四个“?”根本不会帮助清晰。

标签: java html intellij-idea applet noclassdeffounderror


【解决方案1】:
  1. 通过在 DOS 中编译类“假设您使用 Windows”生成 HelloWorld.class,即:javac HelloWorld.java,这将生成 HelloWorld.class - 确保 HelloWorld.class 和 .html 文件在同一个位置,假设在C: 开车。
  2. 更改html页面中的行

    <applet code="/Users/Peterhon/Desktop/HelloWorld.class" width="400" height="100"></applet> 
    

    到这一行:

    <applet code="HelloWorld.class" width="400" height="100"></applet>
    
  3. 如果 Java 安全设置不允许您显示小程序,请在 Windows 控制面板的 Java 控件中的受信任站点中添加 html 文件路径,例如localhost//test.html

【讨论】:

  • 我用过mac,但我已经完成了第一步和第二步,就像你在这里写的一样。但是在mac上不知道怎么做第三步
  • @PeterTsung “但是我不知道如何在mac上进行第三步” 小程序需要经过数字签名才能让公众看到。一旦对小程序进行了数字签名,上述建议的第 3 部分就变得完全无关紧要了。
  • @AndrewThompson 谢谢汤普森。 :-)
猜你喜欢
  • 2020-11-03
  • 2017-02-28
  • 1970-01-01
  • 2020-02-24
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2018-06-01
相关资源
最近更新 更多