Bck2Brwsr
From APIDesign
Bck2Brwsr VM is a Java virtual machine which is capable to take bytecode and convert it (either ahead-of-time e - e.g. during compilation - or just-in-time, e.g. in a browser) into appropriate JavaScript code which does the same thing. As a result one can write in Java, compile with JavaC, process it with Bck2Brwsr (usually via its Maven plugin, but possibly also directly via programmatic APIs) into a JavaScript file(s) that can be executed in any modern browser (IE 10 at least, please).
The easiest way to start is to play with a DEW - the Development Environment for Web. Just visit http://dew.apidesign.org/dew and edit, compile, run in a browser without installing anything or using (now banned) Java plugins.
The Bck2Brwsr project supports the generic and portable HTML/Java APIs as defined by NetBeans. Use them to run your application on desktop as a standalone application, as a NetBeans or Eclipse plugin or in a browser, via Bck2Brwsr VM.
Contents |
Motivation
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.
Licensing
The code of Bck2Brwsr is heavily derived from OpenJDK source code (especially the libraries part, not the HotSpot part). Bck2Brwsr thus needs to match the OpenJDK licensing and be compatible with it. The licensing goal is however the same as in case of OpenJDK:
- if you write your application and execute it against standard JDK libraries - use the classpath exception
- if you hack the Bck2Brwsr VM itself - follow the GPLv2 license
This kind of setup should give Java developers freedom to build their applications and distribute them under conditions of their choose, while making sure modifications, enhancements and improvements of the Bck2Brwsr VM code itself remain publicly available.
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. For example one cannot start new threads (as of Bck2Brwsr 0.13 even there is a proposal to do something with it).
Demo
The current flagship demo is MineSweeper running in the browser: http://xelfi.cz/minesweeper/bck2brwsr/
As a memento it makes sense to keep in mind the little calculator demo showing the first application that I managed to get running in the browser. The application is packaged in a JAR file and the Bck2Brwsr VM loads necessary classes and transforms them into JavaScript on the fly.
There is/was (relatively) enhanced Twitter demo showing various technologies including JSONP communication with the server. However as Twitter abandoned its authorization free API in May 2013, the communication with the server is broken.
For JavaOne2013 I developed a chess application showing use of knockout.js from a Java code. See Knockout4Java.
To demonstrate my geolocation API, I created another Where are you? demo.
We also managed to port JavaFX to Bck2Brwsr and here is a proof of that. There is an overhead to load JavaFX scene graph API, so excuse slight delay when downloading and booting the app. When finally running, the performance should be OK.
Try It Yourself!
It is simple to try Bck2Brwsr! The project skeleton is provided as Maven archetype. Follow steps described at Bck2BrwsrViaCLI or Knockout4Java tutorial.
What is in New Releases?
Next Version 0.55 Will ...
Version 0.54
- Support Class.getPackage() - #45
- Better lambda support - #49
- Ability to process pre-JDK1.6 class files - d2100cf02
Version 0.53
- Bugfix for the Heartbeat support to use it only when connecting to localhost via HTTP
Version 0.52
- Fastforward seven years and updating Closure compiler to version v20210601 - required bunch of fixes of unused variables, wrong code, etc.
- Checking ClassCastException before calling lambda methods
- Using importScripts when Bck2Brwsr is used in a Worker
Version 0.51
Version 0.51 of Bck2Brwsr VM is even better than Bck2Brwsr 0.50:
- Important bugfixes for Gradle --continuous mode
- Improved emulation of java.util.function and java.util.stream packages
Version 0.50
The Bck2Brwsr project has been updated to work with 2021 technologies:
- Gradle task rewritten to be compatible with 7.0
- Support for --continuous Gradle mode has been added
- Issue 19 - support for nest based access control has been implemented
No more RetroLambda. Instead of RetroLambda there is now an abstract IndyHandler interface with two implementations:
- MetafactoryHandler - generates special JavaScript for recognized Java Closures
- StringConcatHandler - converts the indy based string concatenation into simple + of JavaScript strings
The removal of RetroLambda is a good thing - the system was fragile. It relied on reflection and internal behavior of JDKs. However, replacing it with homemade implementation is a request for regressions - finding and supporting all the corner cases is going to take some time. Thanks to Johan Vos, who managed to compile JavaFX with Bck2Brwsr transpiler and reported quite a few bugs. The likehood Bck2Brwsr 0.50 is going to work is now higher!
The easiest way to report an error is to enhance one of existing test-cases:
- SamTest.java for JDK8 closure support
- NestMatesTest.java for JDK11 related functionality
Thanks in advance for trying and helping Bck2Brwsr to get better!
Older Releases
See the full Bck2BrwsrChangeLog for historical information about older releases.
Wishlist
The Bck2Brwsr system is capable to run and execute meaningful applications. Anyway there remains things to be improved and fixed. Any help is welcomed. Just let me know if something interests you:
- Debugger of Java (and not JavaScript) using properly generated source maps would be good
Resources
- Project page at GitHub
- Mailing list and its archive
- As of January 2017 I am moving the development to GitHub - (the original repository remains read-only).
- Maven Repository at java.net
- Javadoc