Using Java8 to build and deploy native HTML5 applications

Ever since I started using TileMill I’ve been interested in ways to develop native applications that are implemented with web technologies.  TideKit appears to be a promising option but I am also wondering if it is a scam.  I purchased a reservation from them a very long time ago and have not received anything from them.  That’s ok though because there’s an old kid on the block who has some new tricks.  Enter Java8’s WebEngine and WebView.  WebView provides you with a fully functional webkit based browser.  It ships with the Java8 runtime so there aren’t any crazy dependencies.  Java8 also has features for deploying applications as native packages/installers.  There is an excellent tutorial on the process of building and deploying a Java8 application here :

To expand on that tutorial I set out to build a simple HTML5 based application, and was pleasantly surprised at the results.

Project structure (using the e(fx)clipse plugin)

java8_html5_app_project_structure starts the app and loads the layout.

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
	private Stage primaryStage;
    private BorderPane rootLayout;
	public void start(Stage primaryStage) {
		try {
			BorderPane root = new BorderPane();
			Scene scene = new Scene(root,400,400);
			this.primaryStage = primaryStage;
		} catch(Exception e) {
	public void initRootLayout() {
        try {
            // Load root layout from fxml file.
            FXMLLoader loader = new FXMLLoader();
            rootLayout = (BorderPane) loader.load();
            // Show the scene containing the root layout.
            Scene scene = new Scene(rootLayout);
            // Initialize the MapController
            MapController controller = loader.getController();
        } catch (IOException e) {
	public static void main(String[] args) {
} ties all of the user interface components together.  The init function shows how to load custom content as well as setup an object for JavaScript to use for upcalls to Java.  The handleGo method shows how to execute javascript inside the browser engine.

package application;
import netscape.javascript.JSObject;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class MapController {
    private Label statusLabel;
    private StringProperty status;
    WebView webView;
    WebEngine engine;
    BooleanProperty loaded = new SimpleBooleanProperty(false);
    Button goButton;
    public void init(){
    	// Make the "Go" button disabled until the page content is loaded
    	// A good example of "computed bindings" :
    	// Bind the status label to a property
    	status = new SimpleStringProperty("");
    	// Get a reference to the WebView's WebEngine
    	engine = webView.getEngine();
		// Add a listener so we can get notified when the page content is loaded
        new ChangeListener<State>() {
            public void changed(ObservableValue ov, State oldState, State newState) {
                if (newState == State.SUCCEEDED) {
		// Give the browser a "java" object that can be used to "upcall" into the Java application.
		JSObject window = (JSObject)engine.executeScript("window");
        window.setMember("java", new JavaApplication());
        // Load the page content
    	String content = "Failed to load content.";
		try {
			InputStream in = this.getClass().getResourceAsStream("resources/map.html");
		    content = IOUtils.toString(in);
		} catch (IOException e) {
    	// You can also do this:
    private void handleGo() {
		try {
			//When the go button is pressed, invoke go.js
			InputStream in = this.getClass().getResourceAsStream("resources/go.js");
		    String script = IOUtils.toString(in);
		} catch (IOException e) {
     * An example class for JavaScript to Java upcalls
    public class JavaApplication {
    	//Using (Double lat, Double lng) did not work:
        public void updateCenter(String lat, String lng) {
            status.set("Center : "+lat+", "+lng);

MapView.fxml defines the application’s layout.  It has a WebView for displaying the app, a Button to test making JavaScript calls from Java, and a Label to demonstrate making upcalls from JavaScript to Java.


<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="" xmlns:fx="" fx:controller="application.MapController">
      <WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
            <Button fx:id="goButton" mnemonicParsing="false" onAction="#handleGo" text="Go!" BorderPane.alignment="CENTER" />
            <Label fx:id="statusLabel" text="statusLabel">
                  <Insets left="12.0" top="5.0" />

Next, for the HTML and JavaScript that make up the web app.  Note how I included these resources in the classpath.  This makes it much easier to load them and ensure they get included in the native package.  map.html is a very simple (Google) map.  It contains some code to update the Java side whenever the map center changes.

<!DOCTYPE html>
    <style type="text/css">
      html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
    <script type="text/javascript"
    <script type="text/javascript">
      var map;
      function initialize() {
        var mapOptions = {
          center: { lat: -34.397, lng: 150.644},
          zoom: 8
        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
        google.maps.event.addListener(map, 'center_changed', function() {
        	//Do an upcall to java with the map center
        	java.updateCenter(map.getCenter().lat(), map.getCenter().lng());
      google.maps.event.addDomListener(window, 'load', initialize);
	<div id="map-canvas"></div>

Finally, go.js is a snippet of JavaScript that moves the map center and adds a marker.

//Center the map on a location
var myLatlng = new google.maps.LatLng(38.536056, -106.000932);
var marker = new google.maps.Marker({
    position: myLatlng,
    map: map,
    title: 'Hello World!'

The end result :