【问题标题】:Running a Java applet on a website在网站上运行 Java 小程序
【发布时间】:2013-04-23 09:38:57
【问题描述】:

我使用 Eclipse 创建了一个 Java 游戏。它有 11 个类(其中一个主类调用其他所有内容)。我以下列方式将其称为小程序:

<!-- This is the applet handler to load the Handler Class to run the game -->
<td colspan="3"><applet code="Handler.class" codebase="History Adventure Alamo Adventure/" name="Alamo Battle Adventure" width="680" height="509" archive="Handler.jar" id="Alamo Battle Adventure">
</applet></td>

上传到网站后,它显示错误并告诉我单击以获取详细信息。当我选择它时,它告诉我 ClassNotFoundException: Handler.class

代码在正文中,而我的脑子里什么都没有。我做错了什么?

这是我的页面代码的其余部分。实际的测试页面可以在 www.kluckhohndesign.com 找到

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script language="javascript" type="text/javascript">
<!--
function popitup(url) {
    newwindow=window.open(url,'name','height=510,width=500');
    if (window.focus) {newwindow.focus()}
    return false;
}

// -->
</script>

<title>Alamo Battle Adventure</title>
</head>

<body>
<blockquote>&nbsp;</blockquote>
<table width="1300" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="318" height="160">&nbsp;</td>
    <td colspan="3"><img src="Alamo Battle Adventure.jpg" width="680" height="160" alt="Top Banner" /></td>
    <td width="318">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td width="226"><a href="howtoplay.html" onclick="return popitup('howtoplay.html')"><img src="How to Play.jpg" width="226" height="52" alt="How to Play" /></a></td>
    <td width="226">&nbsp;</td>
    <td width="227"><a href="Help.html" onclick="return popitup('help.html')"><img src="help.jpg" width="226" height="52" alt="Help" /></a></td>
    <td>&nbsp;</td>
  <tr>
    <td><img src="Alamo Pencil.jpg" width="318" height="250" alt="Alamo Drawing" /></td>
<!-- This is the applet handler to load the Handler Class to run the game -->
    <td colspan="3"><applet code="Handler.jar" codebase="History Adventure Alamo Adventure/" name="Alamo Battle Adventure" width="680" height="509" archive="Handler.jar" id="Alamo Battle Adventure" />
    </applet></td>
    <td><img src="texans.jpg" width="318" height="250" alt="Texans Drawing" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>

【问题讨论】:

  • archive 参数指定 JAR 文件的相对路径。 Handler.jar 和这个网页在同一个目录吗?
  • 你的班级是否在一个包中?所以代码的值应该是“your.package.Handler.class” 代码库也应该是一个 URL。见:docs.oracle.com/javase/1.4.2/docs/guide/misc/applet.html
  • 我的 handler.jar 文件与网页所在目录下的 .class 文件位于同一目录中。 Java 类文件都是用 Eclipse 创建的。我是否需要将它们全部打包以便我只有一个包裹?

标签: java html eclipse applet


【解决方案1】:

这应该会让你走得更远:

<html>
<body>
<applet
    code="Handler"
    codebase="http://www.kluckhohndesign.com/History%20Adventure%20Alamo%20Adventure/"
    name="Alamo Battle Adventure"
    width="680"
    height="509"
    archive="Handler.jar"
    id="Alamo Battle Adventure" >
</applet>
</body>
</html>

您可以省略代码库中的 http://www.kluckhohndesign.com/ 前缀。

现在它会导致InvocationTargetException。但请提出一个新问题。

补充说明

  1. code 属性应该是 class 的完全限定名,并且与 Jar 无关。大多数人认为 FQN 等同于 Handler.class,但实际上它仅意味着 Handler
  2. 如果代码库来自您的站点,则可以简单地使用 History%20Adventure%20Alamo%20Adventure/。它需要%20 用于空格,因为空格在 URL 中无效。再说一次,出于这个原因,大多数部署者根本不会使用带空格的目录名称。
  3. applet 元素从未像您的原始页面中显示的那样“自我关闭”。相反,它需要一个显式的 &lt;/applet&gt; 关闭元素。
  4. 请继续在我建议的更简单的页面中进行测试,不要使用所有其他内容。

【讨论】:

  • 这绝对让我比以前更进了一步。
猜你喜欢
  • 2013-04-09
  • 2019-01-06
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
相关资源
最近更新 更多