'. '

Bck2Brwsr

From APIDesign

(Difference between revisions)
Jump to: navigation, search
(Done, not yet released)
(Done, not yet released)
Line 36: Line 36:
* Communication with server via [[JSON]] and [[JSON]]P. Use '''@OnReceive''' [[annotation]]
* Communication with server via [[JSON]] and [[JSON]]P. Use '''@OnReceive''' [[annotation]]
* [http://xelfi.cz/twttr Twttr demo]
* [http://xelfi.cz/twttr Twttr demo]
 +
* Browser testing harness has nicer output with UL and expandable LI
== TODO ==
== TODO ==

Revision as of 18:19, 14 April 2013

During my duties at JavaOne2012 my feeling (primed by observing various RSS feeds full of posts about something which ends .js) that Java is no longer as cool as it used to be straightened. It always starts by loosing interest of newcomers, targeting just a piece of the technology segment and soon once Good Technology becomes new COBOL. Can that happen to Java? Just read Is JavaScript the Future of Programming? and (if you like Java), you'll get scared. I decided to do something about that. I'd like to return my favorite programming language Bck2Brwsr.

Contents

Goals

Create small Java (check the Bck2BrwsrJavadoc) capable to boot fast and run in 100% of modern browsers including those that have no special support for Java.

Demonstrate that Java has benefits over JavaScript when creating larger HTML5 applications.

Unlike other similar efforts, the goal of this project is not to execute any existing Java library. It is expected that libraries for the new, limited environment need to be specially designed.

Demo

Twitter client showcase. Here is a little calculator demo showing use of knockout.js from a Java code. The application is packaged in a JAR file and the Bck2Brwsr VM loads necessary classes and transforms them into JavaScript on the fly.

Try It Yourself!

The Bck2Brwsr project is publishing artifact snapshots to Maven repository since Feb 13, 2013. That means there is an easy, three steps, way to start coding for the browser yourself. All you need is Java and Maven installed.

Knockout4Java + Bck2Brwsr Archetype

The latest archetype now focuses on integration with Knockout4Java and its execution in Bck2Brwsr (for deployment) and FXBrwsr (for debugging) modes:

$ mvn archetype:generate \
	-DarchetypeGroupId=com.dukescript.archetype \
	-DarchetypeArtifactId=knockout4j-archetype \
	-DarchetypeVersion=0.16
        -Dwebpath=client-web

Possibly replace the 0.16 version with newer version if/when available. Maven will download its libraries. Then it asks few questions (for example name of the project you want to create) and generates the skeleton project for you. When the project is successfully created, switch to the created subfolder and execute following command in it

$ mvn package
$ mvn -f client process-classes exec:exec

to run the application in FXBrwsr mode (suitable for debugging). To run the application in real browser, use:

$ mvn -f client-web clean package -DskipTests bck2brwsr:show

Of course, you don't have to work only from command line. Good IDEs, including NetBeans will recognize the generated project without any struggle and provide you all the comfort you are used to when coding in Java.

Deploying to Server

When your application is ready you can easily deploy it to a server as a set of static pages:

$ mvn clean install
$ unzip client-web/target/*-bck2brwsr.zip
# to test it try firefox
$ firefox public_html/index.html 
# or chrome in a relaxed security mode (needed only on local disk):
$ google-chrome --allow-file-access-from-files public_html/index.html

When unzipped on the server, it is also wise idea to configure it to serve the bck2brwsr.js as a GZiped file. That will get its size to ~200KB.

Eclipse Support

Yesterday we (Jonathan Fuerth watched by me) managed to open the generated (and compiled) project in Eclipse. Showed up without errors after adding target/generated-sources/annotations/ as additional source path root. The support for AnnotationProcessors was limited, when we added new @Property(...), we needed to recompile for the IDE to realize what is going on. However the behavior seems acceptable and certainly better than I expected given the fact nobody tested it and designed for it yet.

Deploy as a Desktop Application

One can also build classical Java desktop application and deploy it to users as a ZIP file:

$ mvn -Pdesktop -f client clean install
$ unzip  client/target/*-javafx.zip
$ java -jar *-app/*jar

and the application starts with a JavaFX WebView and displays your page in it. More info at DukeScript getting started page.

What is in New Releases?

Release 0.5

  • Bck2Brwsr 0.5 has better support for MVVC via Knockout.js - see the calculator demo version 0.5
    • Binds String and primitive types
    • Bind array types (exposed as List
    • Basic binding of complex classes
  • Separate module for Maven archetype called org.apidesign.bck2brwsr:bck2brwsr-archetype-html-sample (and thus instructions for getting Bck2BrwsrViaCLI has changed)
  • Improved speed of Bck2Brwsr virtual machine via better control flow
  • Can use Closure compiler to generate more compact code
    • FULL mode: For batch compilation of everything for now (example pom.xml that uses the j2js goal)
    • MINIMAL mode: Strips spaces. Works in dynamic mode (part of the default Maven archetype)
  • One incompatible change: AnnotationProcessor for the @Page annotation no longer capitalizes field names found in the HTML page. This was meaningful when the fields were static constants. Now (when they are plain instance fields) it makes little sense.

Release 0.4

0.4 is the first release we managed to upload to java.net Maven repository. Heuréka! See Bck2BrwsrViaCLI for simple three steps towards using this version of Bck2Brwsr.

What you can expect? It works. It is sometimes not fast. It is sometimes broken (what would you expect after four months of development?). Errata:

  • When you create new project and want to use it in NetBeans, you need to update nbactions.xml file to refer to 0.4 version, rather than 0.3-SNAPSHOT.

Here is list of achievements for the 0.4 version:

  • Throwing and catching exceptions by Tomáš Z., finally block by me.
  • Support for converting ByteCode in the browser
  • Speed via register based system - Ľubomír finished first version of his register based rewrite on Dec 14, 2012.
  • Speed benchmark and infrastructure to measure it in various environments - Martin Š.
    • Run with -Dvmtest.brwsrs=firefox,chromium-browser (or any other browsers you want to test)
  • Maven archetype for creating the calculator like demos
  • Int32, Int16, Int8 arithmetics done by Martin Š.
  • API for drawing on the canvas: Thanks Toni! Read about his experience using Bck2Brwsr.
  • More precise int64 support - Martin Š. working on
  • Convertor from GWT's native code to Bck2Brwsr's @JavaScriptBody - is sort of there, but not really functional.
  • Fields of same name in subclasses. Thanks to Bck2BrwsrMangling.
  • Compatibility tests can be written with help of @Compare annotation
  • Basic reflection support (e.g. Bck2Brwsr throws SecurityException when allowed),
    • Done: Class.newInstance() works.
    • Done: Class.getMethods() works (returns only public methods)
    • Done: Annotations of classes and methods
  • Support for MVVC like Knockout.js that binds String and primitive types
  • Packages into a static website via JAR files (which then take long time to inflate)
  • Implements java.util.zip APIs

Done, not yet released

  • Better binding of complex classes (defined by a special @Model) annotation
  • Communication with server via JSON and JSONP. Use @OnReceive annotation
  • Twttr demo
  • Browser testing harness has nicer output with UL and expandable LI

TODO

Although the system is capable to run and execute trivial applications, there remains tons of things to improve and fix. Any help is welcomed. Just let me know if something interests you:

  • Use Closure compiler to generate more compact code
  • Method and field overriding with various modifiers
  • More reflection support (e.g. don't throw SecurityException when allowed),
    • No private method/field/constructor/class access
    • Probably no field access
    • May need constructor access
  • Debugger of Java (and not JavaScript would be good)
  • Performance benchmark Sci2000
  • Investigate generating asm.js friendly code
  • Generate Java wrappers for all HTML5 elements dynamically (Honza)

Resources

Personal tools
buy