开发者

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. 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()");
  }


<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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜