Displaying a JavaFX Applet in a Grails Application

I’m going to take a quick look at how to put a JavaFX applet into your Grails application.

Currently there’s only a Grails plug-in for “old-style” Java applets, and not one that enables you to leverage the Java Web Start functionality with a jnlp file.

Fortunately it’s very easy to get a JavaFX Applet into your web application. If you’re not building a Grails application, a lot of steps are still the same for any JEE application. As I found though the web is not awash with JavaFX how-to’s, probably due to it’s poor take up.

So assuming you have already developed your JavaFX applet and you have a jar and a browser jnlp file. If you haven’t, but want something to start working with here is a simple tutorial.

You now need to make a home for your applet files in your Grails application, and a “dist” folder is a good a place as any. So in your “web-app” directory, create a “dist” folder.

Create dist folder

From your JavaFX project, drop your *.jar file and *browser.jnlp file into the “dist” folder. If you’re having trouble finding them using Eclipse, try refreshing your JavaFX project and the should be in a “dist” folder there. You will need to have run the JavaFX script as an Applet rather than an Application in order to create the *browser.jnlp file. You can do this in the Run Configurations in Eclipse.

Once you’ve copied the files you need to update the paths in the jnlp file so it reference its new location, so open it up.

The codebase attribute should be changed so the jnlp tag reads:

<jnlp spec="1.0+" codebase="http://localhost:8080/application_name/dist/" href="Main_browser.jnlp">

and href attribute of the homepage tag.

<homepage href="http://localhost:8080/dist/"/>

Obviously replace localhost with the server you intend to use, and application_name is your actual application name. If you have updated your Config.groovy with … grails.app.context = "/" … so you don’t have use the application name in URLs you should use http://localhost:8080/dist/ instead.

Next you need to make a script declaration in the GSP file where you want the applet to appear:

            archive: "/dist/Main.jar",
            width: 200,
            height: 200,
            code: "package.Main",
            name: "Main"

The archive attribute uses the base url provided in environments block from Config.groovy, so make sure this is correct for your full path. Or if your feeling naughty you can use an absolute path.

<jnlp spec="1.0+" codebase="file:/C:/workspace/JavaFXApp/dist/" href="Main_browser.jnlp">

The width and height attributes should be made similar to the ones defined in your the Stage object of the JavaFX script, otherwise your applet might end up being cropped.

The code attribute needs to have the fully qualified name of the script, so replace “package” with what is appropriate to you.

Next add the script src in the head of your GSP.


Finally you might want to add the following to your grails.mime.types in you Config.groovy.

grails.mime.types = [  
    jnlp: 'application/x-java-jnlp-file'

Your applet will now display in your application; but you can also hit the URL directly and the applet will load using Web Start e.g.


Displaying a JavaFX Applet in a Grails Application