简体   繁体   English

小程序麻烦

[英]Applet troubles

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 . 我有一个名为BHSurveyor.jar的JAR文件,我的类在bhs.gui.GUIApplet中扩展了javax.swing.JApplet 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 (在本地打开的HTML文件中,JAR下面的一个文件夹级别)

<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/ 可以在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 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()");
  }

You did several mistakes. 你犯了几个错误。

  1. archive is must be relative to codebase. archive必须与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 ) 这意味着如果您的jar位于dist文件夹下,您应该说: <applet codebase="/dist/" archive="BHSurveyor.jar"<applet codebase="/" archive="/dist/BHSurveyor.jar" (除非你的jar实际上在/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. 大多数applet标签示例显示它们在code属性的值中写入.class后缀,因此在您的情况下写入:code =“bhs.gui.GUIApplet.class”(正如您在第二个示例中所做的那样,其中codebase和archive似乎是完全的错误。
  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" 虽然大多数HTML元素在写宽度和高度时都支持单位,但我从未见过在applet的情况下支持它,所以删除pxwidth="256" height="256"

Use Applet console. 使用Applet控制台。 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. 使用appletviewer或使用IDE检查applet。 Be sure that it is able to run. 确保它能够运行。
  2. Put it somewhere under web server and check manually URL to its JAR file. 将它放在Web服务器下的某个位置,并手动检查其JAR文件的URL。 You should be able to download jar using browser (or wget command). 您应该能够使用浏览器(或wget命令)下载jar。
  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. 如果这也不起作用,请访问: http//download.oracle.com/javase/1.4.2/docs/guide/misc/applet.html ,从此站点获取applet标记。 Try to understand it. 试着理解它。 Copy/paste it to your HTML and make it working. 将其复制/粘贴到您的HTML并使其正常工作。 Then replace codebase archive to yours and pray :). 然后将codebase存档替换为你的并祈祷:)。

Good luck. 祝好运。

<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. 注意:根据我的统计,这两个applet元素之间存在5个差异。


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). 将其复制/粘贴到浏览器地址栏并按Enter键(对于基于文件的路径,始终是一个很好的“酸性测试”)。

Do you get the directory listing? 你有目录列表吗?

I suspect it should be: 我怀疑它应该是:

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

(ie without localhost ). (即没有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 这意味着/dist/其他代码库实际上会指向

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/ 可以在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 . 似乎BHSurveyor.jar包含文本,至少它返回内容类型的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 在本地测试中更新applet

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. 在本地下载Jar之后(遇到不同的错误,然后安装JDK 7来修复它),我终于能够看到你所描述的内容了。

"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. 首先,请注意您可以使用交叉编译选项来编译版本7 JDK中的Java 1.5(或任何以前的版本)。 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? applet init()方法中是否有任何try / catch语句? Does the applet have a (non-default) constructor? applet是否有(非默认)构造函数? 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? 如果它不是太长或太私密,那么applet源的init()和构造函数部分的代码(复制/粘贴)是什么?

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. 我的最新理论(关于applet加载的主要问题)是有吞咽或忽略异常的try / catch语句。 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. 除此之外,我不知道为什么applet不会加载,但在Java控制台中没有显示任何相关的输出。

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. 顺便说一句,服务器返回Jar的错误内容类型的事实应该被修复,尽管它似乎不是这里的潜在问题。

Are you looking in the Java console? 你在寻找Java控制台吗? For example in IE: Tools->Internet Options->Advanced you'll see an option to enable the console. 例如,在IE:工具 - > Internet选项 - >高级中,您将看到启用控制台的选项。 You may well see some useful error messages there. 您可能会在那里看到一些有用的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM