Embedded HTML 5 browser in Java

Today HTML 5 is one of the most sophisticated and popular UI technology available. Unfortunately writing business code using Javascript is a mess. If you are used to Java’s type safety, the modularity of OSGi and all that dozens of third party libraries, you know that plain Javascript is a huge step backwards. The Java Virtual Machine and HTML complement each other for many years in web based applications. So why not combine both technologies to create rich desktop applications? A platform independent user interface on top of a platform independent programming language – a perfect match!

I was very excited when I heard about the HTML 5 support in JavaFX. However the ‘Webview’ rendering is very bad and not comparable to most recent web browsers. Fortunately the browser support in SWT was improved with Eclipse Luna and embedding XULRunner 24.x is now possible.

XULRunner is the primary HTML rendering engine used by Firefox and is available on all major operating systems. The WebGL support in XULRunner is great, audio and video playback are implemented. Also the rendering result on different platforms is almost identical so you don’t have to fight with all that different browser quirks as usual in web development.

The demo below shows an SWT based application with embedded XULRunner that renders a simple WebGL scene using three.js.

The SWT browser API supports calling Java from Javascript and vice versa. I added the ‘Take Screenshot’ and ‘Add Image’ button just to demonstrate the interaction ability. The screen capture itself is based on the solution I posted a while ago . The source code for the demo is available here.

After you can not rely on a proper XULRunner installed on your user’s target machine, bundle your application together with the appropriate XULRunner version. For OSGi based applications this is really straightforward and an exemplary implementation can be found here (Sorry I couldn’t integrate the OSX version yet).

The org.mozilla.xulrunner bundle is the platform independent host bundle with helper classes to instantiate XULRunner based browsers. The XULRunner binaries are provided by platform specific fragment bundles, one fragment for each operating system characteristic.

 

XULRunner is not supported for GTK3 so under Linux you have to use a start script to run your application:

 
#!/bin/bash
export SWT_GTK3=0
./yourApp

At the moment I’m playing with different approaches to combine Java and HTML. Single page apps with Java calls using AngularJS and Bootstrap, server centric solution using Vaadin and RAP. So far I have absolutely no idea what works best but I let you know in the near future on this blog.


8 Comments on “Embedded HTML 5 browser in Java”

  1. Ming says:

    This is great! I’ve been wanting to dump Swing and JavaFX and just write all my UIs in HTML (but still use Java underneath). How tight is the integration? Can you directly manipulate the DOM and JavaScript objects, or are you limited to just doing eval()?

  2. Jack says:

    Hallo,
    Could you please tell me where did you find the (org.mozilla…) packages. The only available package in mozilla ftp site for eclipse is XulRunner 1.18.

  3. javahacks says:

    Hi sorry for my late response! To answer your question, no there is no official download so you have to package the XULRunner by yourself. Goto https://github.com/javahacks/xulrunner to see how this was done for XULRunner 24. Note that XULRunner 31 support was implemented in Eclipse 4.5 (https://bugs.eclipse.org/bugs/show_bug.cgi?id=437700). Anyway if you are interested in web based desktop applications give the JCEF project a try !

    • Deepan says:

      Did you find JCEF to be superior to XULRunner? In what respect. I have another requirement .. actually I want to be able to debug javascript code hosted inside XULRunner or JCEF. Is that possible.

      Any help is very much appreciated.

      Thanks
      Deepan

      • javahacks says:

        The XULRunner versions supported by SWT are always pretty outdated. If you do not rely on SWT for your main application frame try JCEF or the commercial JXBrowser. Yes and you can debug javascript in JCEF.

  4. Oguz says:

    I’m trying to use this bundle in my osgi-based project. It is working on the eclipse PDE but i cannot make it work independently. It gives this error:

    !ENTRY org.eclipse.osgi 4 0 2015-11-04 15:07:06.684
    !MESSAGE Error starting bundle: initial@reference:file:org.mozilla.xulrunner.win_x86_64_24.0.0.jar
    !STACK 0
    org.osgi.framework.BundleException: A fragment bundle cannot be started: org.mozilla.xulrunner.win_x86_64_24.0.0 [24]
    at org.eclipse.osgi.framework.internal.core.BundleFragment.startWorker(BundleFragment.java:224)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:300)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.startBundle(EclipseStarter.java:1120)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.startBundles(EclipseStarter.java:1113)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.loadBasicBundles(EclipseStarter.java:632)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.startup(EclipseStarter.java:278)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.main(EclipseStarter.java:152)

    Do you have a clue on this, can you help me?

    Thanks in advance.

    • javahacks says:

      What do you mean with ‘it works on the eclipse PDE’? You can launch it from within the Eclipse IDE? Which Eclipse version are you using? Since Eclipse Mars Xulrunner 31 is supported. I planning to adjust my plugins accordingly the next days.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s