Use Java to write application logic; Use HTML5 to render the UI; {@link net.java.html.json.Model Animate an HTML page from Java} (see Duke being rotated by CSS); Use {@link net.java.html.json.OnReceive REST} or WebSockets; interact with JavaScript; Get the best of both worlds! The goal of these APIs is to use full featured Java runtime (like real HotSpot VM), but still rely on a very lightweight rendering technology (so it can potentially fit Bck2Brwsr and definitely to various types of phones). What can be more lightweight (from a browser perspective) than HTML!? By default we use {@link net.java.html.boot.fx JavaFX's WebView} component to display the HTML. We eliminate the need to manipulate the DOM directly, there is a special {@link net.java.html.json Java to Knockout.js binding}. As a result the HTML uses Knockout.js syntax, yet the application code can be written in Java.

Improvements in version 1.3

{@link net.java.html.json.Model Model classes} can generate builder-like construction methods if builder {@link net.java.html.json.Model#builder() prefix} is specified. The JavaFX presenter can be executed in headless mode - just specify -Dfxpresenter.headless=true when launching its virtual machine and no window will be shown. This is particularly useful for testing. Configure your surefire or failsafe plugins like:
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.13</version>
  <configuration>
      <systemPropertyVariables>
          <fxpresenter.headless>true</fxpresenter.headless>
      </systemPropertyVariables>
  </configuration>
</plugin>
OSGi headers are now enterprise OSGi ready.

What's Been Improved in Version 1.2.3?

One can control {@link net.java.html.json.OnReceive#headers() HTTP request headers} when connecting to server using the {@link net.java.html.json.OnReceive} annotation. It is possible to have {@link net.java.html.json.ComputedProperty#write() writable computed properties}. There is an easy way to enable Firebug in the JavaFX based Web View - just run with -Dfirebug.lite=true as this video demonstrates. Bugfix of issues 250503, 252987.

What's New in Version 1.1?

The content of a {@link net.java.html.BrwsrCtx context} can be selected by registering implementations under specific {@link org.netbeans.html.context.spi.Contexts.Id technology identifiers} and requesting them during {@link org.netbeans.html.context.spi.Contexts#newBuilder(java.lang.Object...) construction} of the context. org.netbeans.html:ko4j module's implementation offers ko4j, xhr and websocket identifiers for its registered services (e.g. {@link org.netbeans.html.json.spi.Technology}, {@link org.netbeans.html.json.spi.Transfer} and {@link org.netbeans.html.json.spi.WSTransfer}). org.netbeans.html:ko-ws-tyrus module registers its {@link org.netbeans.html.json.spi.Transfer Java based JSON} and {@link org.netbeans.html.json.spi.WSTransfer WebSocket} implementations under the name tyrus.

A particular DOM subtree that a knockout.js model gets applied to can be selected by using {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String) Models.applyBindings(m, id)} with an id of an HTML element. There is new {@link net.java.html.json.Model#targetId()} attribute which controls behavior of the generated applyBindings method. If specified and non-empty, then the generated method will call {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String)} with this and the provided {@link net.java.html.json.Model#targetId() target id}. If specified, but left empty, then the generated method calls {@link net.java.html.json.Models#applyBindings(java.lang.Object)}. If unspecified, the method will not be generated at all (a change with respect to older versions). However one can still use {@link net.java.html.json.Models#applyBindings(java.lang.Object)} or {@link net.java.html.json.Models#applyBindings(java.lang.Object,java.lang.String)} to perform the association of any model with the page element.

Memory model when using Knockout bindings has been improved (required additions of two new methods: {@link org.netbeans.html.json.spi.PropertyBinding#weak()} and {@link org.netbeans.html.json.spi.FunctionBinding#weak()}) and now the Java {@link net.java.html.json.Model models} can garbage collect, when no longer used. Library writers that use {@link net.java.html.js.JavaScriptBody} annotation can also control garbage collection behavior of method arguments by setting {@link net.java.html.js.JavaScriptBody#keepAlive() keepAlive=false} attribute.

What's New in Version 1.0?

{@link net.java.html.json.Property#array() Array properties} are now mutable from the knockout.js point of view (required {@link org.netbeans.html.json.spi.Proto.Type#replaceValue one SPI change}). The page lookup mechanism can use {@link net.java.html.boot.BrowserBuilder#locale(java.util.Locale) locale} to load localized a page with appropriate suffix. All SPI were moved under the NetBeans namespace - e.g. {@link org.netbeans.html.boot.spi}, {@link org.netbeans.html.context.spi}, {@link org.netbeans.html.json.spi}, {@link org.netbeans.html.sound.spi}, and also {@link org.netbeans.html.json.tck}. Methods annotated with {@link net.java.html.js.JavaScriptBody} annotation and without fallback Java code now throw {@link java.lang.IllegalStateException} with a message suggesting to switch to proper {@link net.java.html.BrwsrCtx#execute browser context} to prevent endless debugging when one forgets to do so.

What's new in older versions? Click the link to view even more historic changes below:

What's New in Version 0.9?

System can run in {@link net.java.html.boot.BrowserBuilder#classloader(java.lang.ClassLoader) Felix OSGi container} (originally only Equinox). {@link net.java.html.json.ComputedProperty Derived properties} now deeply check changes in other {@link net.java.html.json.Model model classes} they depend on and recompute their values accordingly. Knockout.js library has been updated to version 3.2.0.

What's New in 0.8.x Versions?

Setters or array properties on classes generated by {@link net.java.html.json.Model} annotation can be accessed from any thread. {@link org.netbeans.html.sound.spi.AudioEnvironment} can be registered into {@link net.java.html.BrwsrCtx}. There is a {@link net.java.html.json.Models#parse(net.java.html.BrwsrCtx, java.lang.Class, java.io.InputStream, java.util.Collection) method} to parse a JSON array and convert it into {@link net.java.html.json.Model model classes}. Improved behavior of enum values in {@link net.java.html.json.Model knockout bindings}.

Few bugfixes for better portability. New API for {@link net.java.html.boot.script.Scripts headless execution} on top of Nashorn - does not run knockout for Java fully yet (reported as JDK-8046013), however even in current state it is quite {@link net.java.html.boot.script.Scripts useful for testing} of {@link net.java.html.js Java/JavaScript interactions}.

{@link net.java.html.boot.fx.FXBrowsers} has been extended with new helper methods to make it easier to use HTML+Java API in existing JavaFX applications. The annotation processor is made more robust with respect to errors in callback syntax of {@link net.java.html.js.JavaScriptBody} body parameter. Javadoc of {@link net.java.html.BrwsrCtx#execute} method has been improved based on a failure of its usability study. There can be additional parameters to methods annotated by {@link net.java.html.json.OnReceive} that allows one to pass state when a JSON call is made and use it when it finishes. The mechanism of discovery of sibling HTML page has been extended to work on systems that don't support {@link java.lang.Class#getProtectionDomain}.

The first argument of method annotated by {@link net.java.html.json.OnReceive} annotation has to be the associated {@link net.java.html.json.Model model class}.

{@link net.java.html.json.OnReceive} annotation now accepts {@link java.util.List} of data values as second argument (previously required an array).

What's New in 0.7.x Versions?

{@link net.java.html.js.JavaScriptBody} annotation has new attribute {@link net.java.html.js.JavaScriptBody#wait4js()} which allows asynchronous execution. Libraries using {@link net.java.html.js.JavaScriptBody} are urged to use this new attribute as much as possible, as it can speed up execution in certain environments.

Use {@link net.java.html.BrwsrCtx#execute(java.lang.Runnable)} in multi-threaded environment to execute your code on the browser thread. See example {@link net.java.html.BrwsrCtx#execute(java.lang.Runnable) using Java timer}.

Interesting Entry Points

Learn how to {@link net.java.html.json.Model animate an HTML page from Java} without referencing single HTML element from the Java code.

Use {@link net.java.html.json.OnReceive JSON} to communicate with REST based server API.

Use WebSockets and JSON.

Call JavaScript methods from Java and vice versa, via JavaScriptBody.

Getting Started

There are many ways to start developing Html for Java application. However to be sure one chooses the most recent setup, it is recommended to switch to good old command line and use a Maven archetype associated with every version of this project. Make sure at least JDK7 is your installed Java and type:
$ mvn archetype:generate \
 -DarchetypeGroupId=org.apidesign.html \
 -DarchetypeArtifactId=knockout4j-archetype \
 -DarchetypeVersion=0.8 # or newer version, if available
        
Answer few questions (for example choose myfirstbrwsrpage as artifactId) and then you can:
$ cd myfirstbrwsrpage
$ mvn process-classes exec:java
        
In a few seconds (or minutes if Maven decides to download the whole Internet of dependencies) you should see a sample Hello World application rendered in a JavaFX web view component (that of course requires your JDK to come with JavaFX; JDK7 and JDK8 from Oracle contain everything that is needed). The generated application is built around one Java source (uses the {@link net.java.html.json.Model} annotation to auto-generate another Data.java class during compilation) and one HTML file (uses the Knockout syntax to data-bind the HTML elements to the generated Data model):
$ ls src/main/java/**/DataModel.java
$ ls src/main/webapp/pages/index.html
        
That is all you need to get started. Play with the sources, modify them and enjoy Html for Java!

IDE Support

This API is part of NetBeans.org project and as such it works naturally with the NetBeans IDE. On the other hand, the API is using nothing NetBeans specific, it builds on standard Java6 APIs and as such it shall work fine in any IDE.

A lot of work is done by annotation processors that generate various boiler plate code during compilation. This is a standard part of Java since JDK6, but for example Eclipse is known not to deal with processors well and developers using it need to be careful. IntelliJ users hasn't reported any issues and of course, NetBeans IDE support for processors is outstanding.

When using {@link net.java.html.js.JavaScriptBody} annotation, it is useful to do a bit of post processing of classes. There is a Maven plugin for that. NetBeans IDE will invoke it when doing a build. Other IDEs may need some hint to do so. Anyway: If one does not see all (generated) sources or is getting {@link java.lang.LinkageError}s when executing the application, switch to command line and do clean build from there:

$ mvn clean install

If that succeeds, your IDE of choice will hopefully pick the generated sources up and present the result of the build properly. If not, download NetBeans, you will be pleasantly surprised - for example with our excellent Java/JavaScript debugging support.

Deploy Your Application

It is not goal of this documentation to list all possible ways to package and deploy applications which use this API. However it is important for new comers to see the benefits of using the HTML for Java API and as such let's list at least few bundling options, known to work at the time of writing this documentation.

First of all, this is a client technology. You write client applications with it which may, but need not connect to a server. You don't need Tomcat or WebLogic to deploy HTML for Java applications.

The sample project generated by org.apidesign.html knockout4j-archetype is configured to use JavaFX as the rendering technology. This setup is primarily suitable for development - it needs no special packaging, starts quickly and allows you to use classical HotSpot VM debuggers. A final artifact from the build is also a ZIP file which you can use and distribute to your users. Good for desktop applications.

All the HTML for Java libraries are packaged as OSGi bundles and as such they can easily be run in NetBeans as well as in Eclipse. As a result one can use OSGi and have a common module system for both platforms. In addition to that one can render using HTML and have a common UI in both platforms. In such case your application would be packaged as a set of OSGi bundles. Read more...

There is more and more attempts to execute Java bytecode in a browser, without any special Java plugin installed. The HTML for Java is carefully designed to produce lightweight, well performing applications even on such restricted environments. It uses no reflection calls and that allows to statically pre-compile the applications into JavaScript. One of such environments is called Bck2Brwsr, another TeaVM. Both support the {@link net.java.html.js.JavaScriptBody} annotation. Read more or play a minesweeper game packaged for your browser (of course written in Java):

Now when we have seen that the HTML for Java applications can run on any modern browser, we can ask whether they can also fit into a phone!? Yes, they can and especially to phones that can execute Java code already! Just by changing your packaging you can create an APK file and deploy it to your Android phone. Read more... In case you'd like your application to reach out to second biggest group of smartphone users, don't despair: It seems the set of devices that can execute HTML for Java applications has been extended to iPads and iPhones. Get the details here and deploy everywhere!

Convinced it makes sense to use HTML for Java APIs for writing applications that are written once, displayed anywhere? Or do you have an environment which is not supported? In such case you can bring HTML for Java to your environment yourself. Just implement your own {@link org.netbeans.html.boot.spi.Fn.Presenter}!

Other Resources

Duke and HTML5. Together at last! The javadoc for latest and previous versions is also available online: