java 在 Chrome 中加载小程序时 ClassNotFound 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7437641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:58:10  来源:igfitidea点击:

ClassNotFound exception when loading applet in Chrome

javahtmlgoogle-chromeappletclassnotfound

提问by Rolf Staflin

I'm having a hard time getting a Java applet to run in Chrome. The class loader can't find the class, even though the it works fine in Firefox, Opera and Safari.

我很难让 Java 小程序在 Chrome 中运行。类加载器找不到该类,即使它在 Firefox、Opera 和 Safari 中运行良好。

Here's my test applet class (I even took out the package declaration to keep it simple):

这是我的测试小程序类(为了简单起见,我什至去掉了包声明):

import java.awt.Graphics;
import java.applet.Applet;

public class Test extends Applet
{
    public void init() { repaint(); }

    public void paint( Graphics g ) {
        g.drawOval(10, 10, 30, 50);
        g.drawLine(15, 30, 22, 32);
        g.fillOval(28, 28, 7, 5);
        g.drawArc(15, 20, 20, 35, 210, 120);
    }
}

Here's the minimalistic test page:

这是简约的测试页面:

<!doctype html>
<html><head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head><body>
  <p>
    <object type="application/x-java-applet" width="50" height="70">
      <param name="code" value="Test" />
      Test failed.
    </object>
  </p>
</body></html>

Here's the stack trace:

这是堆栈跟踪:

java.lang.ClassNotFoundException: Test
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:252)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:250)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:180)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:161)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:687)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3046)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1498)
    at java.lang.Thread.run(Thread.java:680)

I've compiled the class with javac Test.java, and I put the .class file in the same folder as the .html file. Again, this runs fine in both Firefox, Safari and Opera, so why not in Chrome?

我已经用 编译了类javac Test.java,并将 .class 文件放在与 .html 文件相同的文件夹中。同样,这在 Firefox、Safari 和 Opera 中都运行良好,那么为什么不能在 Chrome 中运行呢?

I've tried creating a jar and adding <param name="archive" value="Test.jar" />, but that didn't help.

我试过创建一个 jar 并添加<param name="archive" value="Test.jar" />,但这没有帮助。

Oh, and while I'm asking stuff: Is there an official spec listing the <param>parameters one can use with applets in <object>tags? It's not in the HTML5 spec, which makes sense, but Oracle seems to favor the old abandoned <applet>tag and I need to use strict HTML5.

哦,虽然我在问东西:是否有官方规范列出了<param>可以与<object>标签中的小程序一起使用的参数?它不在 HTML5 规范中,这是有道理的,但 Oracle 似乎更喜欢旧的废弃<applet>标签,我需要使用严格的 HTML5。

Environment

环境

MacBook Pro running OS X 10.7.1

运行 OS X 10.7.1 的 MacBook Pro

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)

Google Chrome 13.0.782.220

谷歌浏览器 13.0.782.220

采纳答案by Andrew Thompson

To keep it simple for testing, change this:

为了保持简单的测试,改变这个:

<!doctype html>
<html><head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head><body>
  <p>
    <object type="application/x-java-applet" width="50" height="70">
      <param name="code" value="Test" />
      Test failed.
    </object>
  </p>
</body></html>

To this:

对此:

<html>
<head>
  <meta charset="utf-8"/>
  <title>Test</title>
</head>
<body>
  <p>
    <applet code="Test" width="50" height="70">
      Test failed.
    </applet>
  </p>
</body>
</html>

Update 1

更新 1

Note that deployJava.jsis the correct way to embed an applet these days. It writes an object or embed element as the browser needs it, and it aims to do it correctly.

请注意,deployJava.js是当今嵌入小程序的正确方法。它根据浏览器的需要编写一个对象或嵌入元素,它的目标是正确地完成它。

回答by andreotti

Looks like it just doesn't see your applet.

看起来它只是没有看到您的小程序。

  1. The archive parameter is mandatory The path to your jar has to be specified correctly.
  2. If you don't specify the path, the assumption is it's in the same directory as your testpage, and not a subdir.
  1. 存档参数是必需的 必须正确指定 jar 的路径。
  2. 如果您不指定路径,则假设它与您的 testpage 位于同一目录中,而不是子目录。

And we also assume, that jar contains your Test.class :)

我们还假设,该 jar 包含您的 Test.class :)