How to display java applet inside GWT page?

2019-02-14 14:23发布

问题:

I'm probably missing something simple here, but I can't find the answer elsewhere. I just want to display an applet in my GWT code.

OS: Windows XP Java: JDK 1.6.0_10 Other: GWT, GWT-Ext 2.0.5

Here is the applet (obviously simplified for testing):

package foo.applet;

import javax.swing.JApplet;
import java.awt.Graphics;

public class HelloApplet extends JApplet 
{
    public void paint(Graphics g) 
    {
        g.drawRect(0, 0, 
                   getSize().width - 1,
                   getSize().height - 1);
        g.drawString("Hello world!", 5, 15);
    }
}

Here is the code calling it:


package foo.applet;

import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.widgets.Panel;


public class AppletPanel extends Panel 
{
public AppletPanel()
{
    HTML applet = new HTML();
    applet.setHTML("<applet name=\"HelloApplet\" code=\"HelloApplet.class\" width=\"300\" height=\"300\"" );
    this.add(applet);
}

}

When I launch the app in hosted mode, the jvm crashes (filed incident 1425130 with Sun).

When I try to compile the GWT code for running in a browser, I get this:

        [ERROR] Errors in 'file:/C:/<blah>/applet/HelloApplet.java'
           [ERROR] Line 3: The import javax.swing cannot be resolved
           [ERROR] Line 4: The import java.awt cannot be resolved
           [ERROR] Line 6: JApplet cannot be resolved to a type
           [ERROR] Line 8: Graphics cannot be resolved to a type
           [ERROR] Line 11: The method getSize() is undefined for the type HelloApplet
           [ERROR] Line 12: The method getSize() is undefined for the type HelloApplet

Obviously I'm missing some applet library, but I've grepped through all the jars in the jdk and tried including all of the ones that list JApplet or awt (plugin.jar, resources.jar, rt.jar, deploy.jar, javaws.jar).

Also, I'm pretty sure once I solve this problem there's another one lurking right after it, but I'll save that for another question.

Thanks!


The legacy app is not an applet - it is a thick-client Swing application. We've hacked it to run as an applet because our customers want a browser client and this is the fastest way to get that done.

I don't know if GWT would accept the JPanel solution - the app is not written in any way that GWT can parse - i.e. it's not using the GWT API, it's using the Swing API. AFAIK, the only way to mix Swing with GWT would be in an applet fashion.

Am I missing something?

回答1:

Are you trying to GWT-compile your applet?

This won't work, as GWT compilation (which is just translation from Java to Javascript) supports only handful of Java libraries and certainly not applets.

Make sure that your applet is not on GWT source path (move it to another package).

Reference: http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=RefJreEmulation



回答2:

Don't use the GWTCompiler to compile your applet code. I would recommend creating a second module (or project) that contains only the applet code. Compile this to a separate JAR using the standard Javac compiler (or your IDE/ant)

The GWTCompiler uses a subset of the Java libraries, and should only be used to generate the code which is going to eventually run as Javascript.



回答3:

Google found this. One of the responses says: "The previous poster is right, the shell can not handle embedded things like Flash or Applets. There are some restrictions in the SWT component used to run the browser inside of the shell. A bug report has been associated with this issue, you may want to keep an eye on it for future updates."

Looks like it can't be done.



回答4:

I found this during research for a gwt app, and even though this is an old thread thought i would post a method to run an applet inside of gwt.

First create two distinct projects, one for your applet and one for gwt. make your applet as normal.

jar your applet.

then SIGN your .jar with jarsigner.

create your gwt modules as normal.

to embed the applet I use a gwt HTML object with an applet tag like this:

(applet MAYSCRIPT code = 'com.myapplet.MyApplet' id ='myApplet' jnlp_href = '/spplets/MyApplet.jnlp', width=500, height=400)(/applet)

Simply add the HTML widget to a contentPanel and the gui part is done.

The applet .jar will need to go in the /war for your gwt project.

Along with the .jar you will need to write a .jnlp file to launch the applet.

This will embed an applet in gwt and run it in hosted or production mode. The key is to SIGN your .JAR and launch it with .JNLP



回答5:

A light-heavyweight app might mix GWT and JNLP. Then we could get the bigger jars onto people's machines rather transparently. As an example, I'd like to use the Batik toolkit or other SVG-related goodies to have SVG in my GWT app, rather than being forced to use only png or other raster formats.

  • Tyro - you can also mail your thoughts on this to me at bob.futrelle@gmail.com


回答6:

"The import javax.swing cannot be resolved" - sorry, I'm not a GWT maven, but this error is classpath-esque. Sounds like GWT can't find the rt.jar for your JVM.