小程序的烦恼

发布于 2024-11-30 16:09:27 字数 3604 浏览 0 评论 0 原文

我正在尝试在我的网站中嵌入一个小程序,但看在上帝的份上,我无法弄清楚为什么这行不通!我有一个名为 BHSurveyor.jar 的 JAR 文件,其中的类扩展了 bhs.gui.GUIApplet 中的 javax.swing.JApplet。现在,我已经尝试将它嵌入到页面中,并且我已经尝试了从

<applet codebase="/dist/" archive="/dist/BHSurveyor.jar" code="bhs.gui.GUIApplet" width="256px" height="256px" />

(在 JAR 下一级文件夹级别的本地打开的 HTML 文件中)到

<applet codebase="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/" archive="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/BHSurveyor.jar" code="bhs.gui.GUIApplet.class" width="256px" height="256px" />

其所有排列,然后是一些......但是每一次,我收到相同的红色“错误。单击了解详细信息”消息(未提供任何详细信息)。 有人可以告诉我在这里做什么吗?

我的设置


可以在 http://s.supuhstar.operaunite.com/s/content/test/

测试


按照建议,我跑了

jar -tvf BHSurveyor.jar
'jar' is not recognized as an internal or external command,
operable program or batch file.

假设他们意味着别的东西,我尝试了

java -jar -tvf BHSurveyor.jar
Unrecognized option: -tvf
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

所以你看这并不没有帮助...再次,逃离假设,我运行了这个并得到了我希望的预期输出:

"C:\Program Files\Java\jdk1.7.0\bin\jar" -tvf BHSurveyor.jar
     0 Mon Aug 29 11:56:10 EDT 2011 META-INF/
   228 Mon Aug 29 11:56:08 EDT 2011 META-INF/MANIFEST.MF
     0 Mon Aug 29 11:56:10 EDT 2011 bhs/
     0 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/
  2264 Mon Aug 29 11:56:08 EDT 2011 bhs/AnswerType.class
  1851 Mon Aug 29 11:56:08 EDT 2011 bhs/DefPage.class
  1965 Mon Aug 29 11:56:10 EDT 2011 bhs/DefQuestion.class
  1463 Mon Aug 29 11:56:10 EDT 2011 bhs/DefTitlePage.class
  1338 Mon Aug 29 11:56:10 EDT 2011 bhs/Main.class
   615 Mon Aug 29 11:56:08 EDT 2011 bhs/Page.class
   786 Mon Aug 29 11:56:10 EDT 2011 bhs/Question.class
   402 Mon Aug 29 11:56:10 EDT 2011 bhs/TitlePage.class
  1757 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils$1.class
   766 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils$2.class
  6188 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils.class
  3409 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIApplet.class
  1218 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIJFrame.class
  3549 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIPage.class
   824 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIQuestion$1.class
  8650 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIQuestion.class

Applet 构造函数和 init


  public GUIApplet(Page... pgs)// throws UnknownHostException
  {
    System.out.println("new GUIApplet(Page... " + pgs + ")");
//    System.out.println(java.net.InetAddress.getLocalHost().getHostAddress());
    System.out.println("  setting layout...");
    setLayout(new java.awt.GridBagLayout());

    System.out.println("  adding components...");
    addComponents();

    System.out.println("  initializing pages...");
    pages = new ArrayPP<>();

    System.out.println("  installing pages...");
    int i=1;
    for (Page p : pgs)
      if (p != null)
        installPage(p);
    System.out.println("  changing look-and-feel...");
    LookAndFeelChanger.setLookAndFeel(LookAndFeelChanger.NIMBUS);
    System.out.println("  fixing nav buttons...");
    fixNavButtons();
    System.out.println("GUIApplet complete");
  }

  public void init()
  {
    // TODO start asynchronous download of heavy resources
    System.out.println("GUIApplet.init()");
  }

I'm trying to embed an applet in my site, but I can't for the love of God figure out why this won't work! I have a JAR file called BHSurveyor.jar with my class that extends javax.swing.JApplet in it at bhs.gui.GUIApplet. Now, i've already tried embedding it in a page, and I've tried everything from

<applet codebase="/dist/" archive="/dist/BHSurveyor.jar" code="bhs.gui.GUIApplet" width="256px" height="256px" />

(in a locally opened HTML file one folder level below the JAR) to

<applet codebase="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/" archive="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/BHSurveyor.jar" code="bhs.gui.GUIApplet.class" width="256px" height="256px" />

and all permutations thereof, and thensome... but each and every time, I get the same red "Error. Click for details" message (which gives no details). Can someone tell me what to do, here?

My setup


A full recreation of my setup can be viewed at http://s.supuhstar.operaunite.com/s/content/test/

Testing


As suggested, I ran

jar -tvf BHSurveyor.jar
'jar' is not recognized as an internal or external command,
operable program or batch file.

Assuming they meant something else, I tried

java -jar -tvf BHSurveyor.jar
Unrecognized option: -tvf
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

So you see this doesn't help... Again, running from assumptions, I ran this and got what I hope was the expected output:

"C:\Program Files\Java\jdk1.7.0\bin\jar" -tvf BHSurveyor.jar
     0 Mon Aug 29 11:56:10 EDT 2011 META-INF/
   228 Mon Aug 29 11:56:08 EDT 2011 META-INF/MANIFEST.MF
     0 Mon Aug 29 11:56:10 EDT 2011 bhs/
     0 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/
  2264 Mon Aug 29 11:56:08 EDT 2011 bhs/AnswerType.class
  1851 Mon Aug 29 11:56:08 EDT 2011 bhs/DefPage.class
  1965 Mon Aug 29 11:56:10 EDT 2011 bhs/DefQuestion.class
  1463 Mon Aug 29 11:56:10 EDT 2011 bhs/DefTitlePage.class
  1338 Mon Aug 29 11:56:10 EDT 2011 bhs/Main.class
   615 Mon Aug 29 11:56:08 EDT 2011 bhs/Page.class
   786 Mon Aug 29 11:56:10 EDT 2011 bhs/Question.class
   402 Mon Aug 29 11:56:10 EDT 2011 bhs/TitlePage.class
  1757 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils$1.class
   766 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils$2.class
  6188 Mon Aug 29 11:56:10 EDT 2011 bhs/Utils.class
  3409 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIApplet.class
  1218 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIJFrame.class
  3549 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIPage.class
   824 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIQuestion$1.class
  8650 Mon Aug 29 11:56:10 EDT 2011 bhs/gui/GUIQuestion.class

Applet constructor and init


  public GUIApplet(Page... pgs)// throws UnknownHostException
  {
    System.out.println("new GUIApplet(Page... " + pgs + ")");
//    System.out.println(java.net.InetAddress.getLocalHost().getHostAddress());
    System.out.println("  setting layout...");
    setLayout(new java.awt.GridBagLayout());

    System.out.println("  adding components...");
    addComponents();

    System.out.println("  initializing pages...");
    pages = new ArrayPP<>();

    System.out.println("  installing pages...");
    int i=1;
    for (Page p : pgs)
      if (p != null)
        installPage(p);
    System.out.println("  changing look-and-feel...");
    LookAndFeelChanger.setLookAndFeel(LookAndFeelChanger.NIMBUS);
    System.out.println("  fixing nav buttons...");
    fixNavButtons();
    System.out.println("GUIApplet complete");
  }

  public void init()
  {
    // TODO start asynchronous download of heavy resources
    System.out.println("GUIApplet.init()");
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听你说爱我 2024-12-07 16:09:27
<applet 
    codebase="/dist/" 
    archive="/dist/BHSurveyor.jar" 
    code="bhs.gui.GUIApplet" 
    width="256px" 
    height="256px" />

将其更改为..

<applet 
    codebase="dist/" 
    archive="BHSurveyor.jar" 
    code="bhs.gui.GUIApplet" 
    width="256" 
    height="256" >
</applet>

注意:根据我的统计,这 2 个 applet 元素之间有 5 个差异。


代码库

<applet 
    codebase="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/" 
    ..

对代码库绝对路径的一些仔细检查促使我做出进一步的评论。

我怀疑是否存在如图所示的路径。将其复制/粘贴到浏览器地址栏中,然后按 Enter 键(对于基于文件的路径始终是一个很好的“严格测试”)。

你得到目录列表了吗?

我怀疑它应该是:

file:///C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/

(即没有localhost)。

代码库或类似路径中的初始 / 也具有特殊意义。它相当于“网站的根”。这意味着您的 /dist/ 的其他代码库实际上会指向

file:///C:/dist/

您会得到什么结果浏览器中的该路径?

顺便说一句 - 这些问题都不是反问。为了最好地帮助你,我需要答案。


测试页面更新

可以在 http://s.supuhstar 上查看我的设置的完整重现.operaunite.com/s/content/test/

看起来好像 BHSurveyor.jar 包含文本,至少是 返回内容类型text/html。并且控制台非常有帮助..

java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.InstantiationException: bhs.gui.GUIApplet
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.InstantiationException: bhs.gui.GUIApplet
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    ... 15 more
Exception: java.lang.reflect.InvocationTargetException

在本地测试中更新小程序

在本地下载 Jar 后(遇到不同的错误,然后安装 JDK 7 来修复它),我终于能够看到您所描述的内容了。

“错误。单击查看详细信息”消息(未提供详细信息)。

  1. 首先,请注意,您可以使用交叉编译选项在版本 7 JDK 中编译 Java 1.5(或任何先前版本)。一个方便的提示,可帮助您了解何时需要进行部署。
  2. 但最重要的是..
    • 小程序 init() 方法中是否有任何 try/catch 语句?小程序是否有(非默认)构造函数?如果是这样,请重新尝试/捕获相同的问题。
    • 如果不是太长或太私密,小程序源代码的 init() 和构造函数部分的代码(复制/粘贴)是什么?

我的最新理论(关于小程序加载的主要问题)是,有 try/catch 语句吞并或忽略异常。除此之外,我不知道为什么小程序无法加载,但在 Java 控制台中没有显示任何相关输出。

顺便说一句,服务器返回 Jar 的错误内容类型的事实应该得到修复,尽管这似乎不是这里的根本问题。

<applet 
    codebase="/dist/" 
    archive="/dist/BHSurveyor.jar" 
    code="bhs.gui.GUIApplet" 
    width="256px" 
    height="256px" />

Change that to..

<applet 
    codebase="dist/" 
    archive="BHSurveyor.jar" 
    code="bhs.gui.GUIApplet" 
    width="256" 
    height="256" >
</applet>

Note: By my count there are 5 differences between those 2 applet elements.


Codebase

<applet 
    codebase="file://localhost/C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/" 
    ..

Some close examination of that codebase absolute path prompted me to make further comments.

I doubt there is any such path as shown. Copy/paste it into a browser address bar and hit enter (always a good 'acid test' for file based paths).

Do you get the directory listing?

I suspect it should be:

file:///C:/Users/Supuhstar/Documents/NetBeansProjects/BHSurveyor/dist/

(i.e. without localhost).

There is also a special significance to the initial / in a codebase or similar path. It equates to 'the root of the site'. That means that your other codebase of /dist/ would actually point to

file:///C:/dist/

What result do you get for that path in the browser?

BTW - neither of those questions are rhetorical. To best help you, I need answers.


Update on test page

A full recreation of my setup can be viewed at http://s.supuhstar.operaunite.com/s/content/test/

It appears as though BHSurveyor.jar contains text, at least it is returning a content-type of text/html. And the console is being very helpful..

java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.RuntimeException: java.lang.InstantiationException: bhs.gui.GUIApplet
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.InstantiationException: bhs.gui.GUIApplet
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    ... 15 more
Exception: java.lang.reflect.InvocationTargetException

Update on applet in local testing

After downloading the Jar locally, (encountering a different error, then installing JDK 7 to fix it), I am finally in a position to see what you described.

"Error. Click for details" message (which gives no details).

  1. Firstly, note that you can use the cross-compilation options to compile a Java 1.5 (or whatever previous version) in the version 7 JDK. A handy tip to know when it comes time for deployment.
  2. But most importantly..
    • Are there any try/catch statements in the applet init() method? Does the applet have a (non-default) constructor? If so, same question re try/catch.
    • If it is not too long or too private, what is the code (copy/pasted) of the init() and constructor parts of the applet source?

My latest theory (as to the primary problem with the applet loading) is that there are try/catch statements that are swallowing or ignoring exceptions. Beyond that, I am at a loss as to why the applet would not load, yet not show any relevant output in the Java Console.

As an aside, the fact that the server is returning the wrong content-type for the Jar should be fixed, though it seems it is not the underlying problem here.

墨小墨 2024-12-07 16:09:27

你犯了几个错误。

  1. archive 必须与代码库相关。这意味着如果您的 jar 位于 dist 文件夹下,您应该说: < ;applet codebase="/" archive="/dist/BHSurveyor.jar" (除非您的 jar 实际上位于 /dist/dist/BHSurveyor.jar 下)
  2. 大多数小程序标签示例显示他们在 code 属性的值中写入 .class 后缀,因此在您的情况下写入: code="bhs.gui.GUIApplet.class" (正如您在第二个示例中所做的那样,其中 codebase 和 archive虽然
  3. 大多数 HTML 元素在写入宽度和高度时都支持统一,但我从未见过小程序支持这种情况,因此删除 px: width="256" height =“256”

使用小程序控制台。您可能会看到某种异常,这将帮助您了解问题所在,

请使用以下

  1. 命令检查小程序 。 >appletviewer 或使用您的 IDE。确保它能够运行。
  2. 将其放在 Web 服务器下的某个位置并手动检查其 JAR 文件的 URL。您应该能够使用浏览器(或 wget 命令)下载 jar。
  3. 如果这也不起作用,请访问:http:// /download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html,从此站点获取 applet 标记。尝试去理解它。将其复制/粘贴到您的 HTML 中并使其正常工作。然后将代码库存档替换为您的并祈祷:)。

祝你好运。

You did several mistakes.

  1. archive is must be relative to codebase. It means that if your jar is under dist folder you should say: <applet codebase="/dist/" archive="BHSurveyor.jar" or <applet codebase="/" archive="/dist/BHSurveyor.jar" (unless your jar is actually under /dist/dist/BHSurveyor.jar)
  2. Most applet tag examples show that they write .class suffix in the value of code attribute, so in your case write: code="bhs.gui.GUIApplet.class" (as you did in second example where codebase and archive seem to be completely wrong.
  3. Although most HTML elements support unites when writing width and height I have never seen that this is supported in case of applets, so remove px: width="256" height="256"

Use Applet console. It can be started from menu of your browser. Probably you will see some kind of exception there that will help you to understand what the problem is.

If all this does not help, do the following.

  1. Check you applet with appletviewer or using your IDE. Be sure that it is able to run.
  2. Put it somewhere under web server and check manually URL to its JAR file. You should be able to download jar using browser (or wget command).
  3. If this does not work too, go there: http://download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html, take the applet tag from this site. Try to understand it. Copy/paste it to your HTML and make it working. Then replace codebase archive to yours and pray :).

Good luck.

梦巷 2024-12-07 16:09:27

您在查看 Java 控制台吗?例如,在 IE 中:工具 -> Internet 选项 -> 高级,您将看到启用控制台的选项。您可能会在那里看到一些有用的错误消息。

Are you looking in the Java console? For example in IE: Tools->Internet Options->Advanced you'll see an option to enable the console. You may well see some useful error messages there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文