Brennan Spies's blog

JavaScript Support in Java 6 Update 10

Tagged:  

The minor release with major features, Java 6 Update 10 brought a host of improvements to the long-neglected Java applet. One of these improvements was better interoperability with JavaScript, which was delivered to the new Java plug-in in the form of LiveConnect technology. As Sun's entrant into the RIA race, JavaFX, comes closer to a 1.0 release, such interoperability is crucial to the success of this effort.

While it has always been possible (to a certain extent) to call between an applet and JavaScript running the same browser, LiveConnect vastly improves the technology by

  • Improving reliability. A number of bugs involving the stability of the browser when making JavaScript<-->Java calls have been fixed.
  • Improving performance. Dramatically better performance...always a good thing.
  • Making it more consistent across different browsers. Support for JavaScript interoperability was always a little better on Mozilla browsers, but now the functionality is more consistent across different browsers with the new plug-in.
  • Increasing the functionality. It's now possible to instantiate new Java objects from JavaScript, call methods on them, as well as reference third-party packages on all browsers.

Calling Java from JavaScript

The following code shows how to invoke methods on an applet through JavaScript:

Java:

package org.example;

public class MyApplet extends Applet {
     public String getMessage() {
          return "Hello World!";
     }
}

HTML/JavaScript:

<applet id="myApp" archive="helloworld.jar" code="org.example.MyApplet"/>

<script type="text/javascript">
     var msg = myApp.getMessage();
     alert(msg);
</script>

Public fields in the Java class can also be called from JavaScript, as well as static fields and methods. Due to a new synthetic keyword, Packages, that is now attached to every applet, it is now possible to instantiate/call methods on classes that are not applets. For example, if we add a new class in the org.example package,

package org.example;

public class MyNonApplet {
     public String getOtherMessage() {
          return "This is NOT a hello world!";
     }
}

we can now create an instance of this class from the JavaScript and do things with it. Using the example above, let's modify the script a little:

<script type="text/javascript">
     var other = new myApp.Packages.org.example.MyNonApplet();
     alert(other.getOtherMessage());
</script>

This opens up the intriguing possibility of accessing a rich set of Java APIs from client-side JavaScript. Take, for example, the ability to add SVG support in (ahem) the only major browser that does not support it*. With Apache Batik and the Java plug-in, full support of SVG in the browser--SVG JavaScript API and all--becomes a reality. (Batik 1.7, by the way, is one of the most compliant implementations of SVG. And SVG might just be a faster way into the web designer's heart than JavaFX, but I digress...)

Each time a call into an applet is made from JavaScript, it is handled by a worker thread that is associated with the applet (there is only one per applet).

Calling JavaScript from Java

All instances of JavaScript objects within Java code appear as instances of netscape.javascript.JSObject. There are essentially two ways of invoking JavaScript from Java:

  1. Calling the static JSObject.getWindow(Applet arg) method with the applet object passed in as an argument. This method returns a JSObject that represents the DOM Window. This JSObject can in turn be used to call() JavaScript functions or eval() JavaScript code. The API for JSObject is here.
  2. Add a method in your Java code that takes JSObject as a formal parameter. Communication can then be bootstrapped from the JavaScript side by calling this Java method and passing a JavaScript object as an argument. It's a little less convenient than option 1, but useful if some JavaScript code wants to delegate some work to the Java code.

Furthermore, you can actually modify the HTML DOM from the Java side using a common DOM API. For the purposes of compiling Java code written with references to JSObject, you would place the jre/lib/plugin.jar on your classpath. Note also that, since JavaScript is a single-threaded language, communication from Java to JavaScript is restricted to one thread at a time; so you should structure your code accordingly.

Other JVM Languages

Of course, the other interesting possibility is the idea of languages like Scala, JRuby, Groovy, Clojure, or Fan being able to run in the browser with support for JavaScript interaction. A truly sick mind (and you know who you are) might use the applet merely as a trojan horse in order to get his/her favorite language running in the browser. I'll leave it at that!

Sun's renewed interest in the applet--sparked in part by RIA and in no small way by the resurgence of alternative browsers--has opened a door many thought closed a long time ago. Interesting times, indeed.

* Even worse now that the Adobe SVG plug-in has been discontinued.

Sun Releases Java 6 Update 10

Tagged:  

Sun Microsystems has released Java 6 Update 10, the so-called "consumer JRE" because of its ability to be deployed in a lighter-weight footprint (the "Java Kernel" or the core JRE classes) and download other classes on the fly as needed. The move is considered a necessary precursor to Sun's push into the RIA (Rich Internet Applications) space, where the accumulated bulk of the JRE is considered a drawback to faster, easier end-user distribution. Many of the improvements in Java 6u10 also pave the way for the future release of JavaFX technologies.

The New Java Plug-In

The Java Kernel is the key to the "Next Generation Java Plug-In Technology" for web browsers, which makes its debut in Java 6 Update 10. The most significant improvement in the new Java plug-in is the ability to launch applets from Java Network Launch Protocol (JNLP) files. This allows Java applets to reuse some of the extensions which had been previously available only for JNLP/Java Web Start technology, including access to JavaFX runtime libraries, support for 3D graphics via OpenGL, persistent data storage, and local filesystem storage. Other improvements in the Next-Gen Plug-In include better Java to JavaScript interoperability, a unified deployment mechanism for JNLP and applets, multiple JRE version support, classloader caching, new parameter arguments to <applet>,<object>, and <embed> tags, as well as numerous small improvements to the reliability and usability of applets. A full list of the new features is here.

Java Quick Starter

Also new to Update 10 is the Java Quick Start service, which preloads portions of the JRE onto the system disk cache, substantially decreasing the startup time. It is turned on by default in Windows 2000 and XP systems, but turned off in Vista since Vista offers its own preloading mechanisms.

Nimbus Look and Feel

Update 10 also comes with a new Swing look and feel, Nimbus. Nimbus gives a much needed face-lift to some of the stock L&F's available in the base platform with a richer, more attractive set of widgets.

Other Items of Note

Patch in Place. Update 10 brings the ability to do a patch in place when updating the JRE/JDK, allowing the simple upgrading of your installation.

Direct3D Support. On Windows platforms, the Direct3D pipeline has been completely revamped, offering much-improved rendering of Swing or Java2D applications which rely on alpha compositing, gradients, arbitrary transformations, filtering and other more advanced 2D operations. This upgrade relies on hardware acceleration (video cards) as well as the DirectX 9c runtime.

A complete set of notes on the changes in Java 6 Update 10 is available here.

WebKit Browser Passes Acid 3 Test

Tagged:  

The WebKit browser project, upon which the Google Chrome and Apple Safari browsers are based, has just announced that their browser has become the first browser to pass all of the Acid 3 tests.

The third revision of the Acid tests, promoted by the Web Standards Project as a way of ensuring browser compatbility with web standards, includes tests of the following standards:

DOM2: Core, Events, HTML, Range, Style, Traversal, Views
ECMAScript
HTML4
HTTP
Media Queries
Selectors (:lang, :nth-child(), combinators, dynamic changes, …)
XHTML 1.0
CSS2 (@font-face)
CSS2.1 (’inline-block’, ‘pre-wrap’, parsing…)
CSS3 Color (rgba(), hsla(), …), UI (’cursor’)
data: URIs
SVG

The Acid 3-passing version of WebKit is available as a download of the nightly build.

Java API for RESTful Web Services Now Final

Tagged:  

The Java API for RESTful Web Services, JAX-RS (also known as JSR-311), has been approved by the final ballot of the SE/EE Executive Committee.

JAX-RS allows you to use @GET, @POST, @PUT, @DELETE, and other annotations to describe RESTful style web services in a manner similar to the way that JAX-WS does for SOAP and WSDL-based web services.

The java.net project page for JSR-311 is here. The reference implementation is called the Jersey project, but other implementations exist in the Restlet framework, JBoss RESTeasy project, and Apache CXF project.

Jumping in Java

Tagged:  

Anyone who has ever programmed (hopefully briefly) in some type of assembly language has gone to the bare metal in implementing loops, if statements, and other programming language features. When programming in assembly, these constructs use some form of J* jump instruction together with labels to accomplish the task. In higher-level languages, jump instructions are usually associated with the infamous and properly banned goto statements (which are unrestricted jumps), but restricted jumps are far more common: return statements, the throw keyword (for exceptions), and of course the standard loops, if statements, and the like.

Being mainly a Java programmer, I occasionally miss language features such as continuations and co-routines. I came upon just such a situation recently, and I wondered if it was possible to imitate a continuation in Java without needing to resort to more sophisticated techniques such as Javaflow or RIFE's own continuation library. What I found out was that I could, albeit in a limited way.

Google Retracts Objectionable EULA Language

Tagged:  

In all the excitement surrounding the Chrome release, a few people quickly noticed some disturbing language in the EULA agreement:

11.1 You retain copyright and any other rights that you already hold in Content that you submit, post or display on or through the Services.

As Matt Cutts of Google blogged, Google has now retracted that language from Chrome's EULA agreement.

You are now free to post blogs (including this one) without consulting Google's legal team. *Whew*.

Google Chrome

Yesterday, the official Google blog broke the news that, yes, Google is indeed developing its own browser called Google Chrome. Much of the motivation and thinking behind this move is described interesting enough in an online comic strip.

Early news:

  • It is based on WebKit, the open source engine behind Safari
  • The browser runs with a JavaScript virtual machine codenamed "V8" that was developed from scratch by a team in Denmark
  • Each tab in the browser will represent a separate process, and will have its own controls and address bar
  • Google's Gears project will be bundled with the browser
  • The browser will have multiple tabs and an auto-complete address bar (like Firefox)
  • There will be a "privacy mode" where no traces of browsing history are left on the computer
  • Web applications can be launched in their own window (sans address bar, etc), an "RIA" feature
  • Built in protection from phishing and malware

According to the blog entry the beta version of Chrome should be available sometime today at this page, but as of this writing the URL just returns a "Page Not Found" (404).

Update: Chrome is now available at the above link.

JRuby 1.1.4 Released

Tagged:  

The JRuby project, an implementation of the Ruby language on the JVM, has released version 1.1.4. Though a minor (maintenance) release, the new version introduces a number of improvements, including:

  • A substantially re-written Ruby-Java integration layer.
  • Big improvements in performance: about 2-20x.
  • Reduction of garbage collection cycles (efficient memory usage).
  • Experimental support for Ruby 1.9 (via the --1/9 flag)
  • Started support for the win32 API
  • Ported Rubinius' FFI subsystem for integrating with C libraries
  • Addition of OSGi metadata to the jruby.jar
  • Lots of bug fixes (72 issues resolved since version 1.1.3)

The full release notes are here.

You can download the new version here.

GWT 1.5 Released

The Google Web Toolkit (GWT) project has released version 1.5 of its Java-based web development library yesterday. The new release sports a host of new features such as:

  • Support for Java 5 language features (generics, enums, enhanced for loops, and static imports)
  • Improved performance of compiled JavaScript code
  • It is now possible to sublcass the JavaScriptObject class, creating "class overlays" that can be mapped to arbitrary JavaScript classes
  • Expanded JRE emulation library
  • A new high-performance DOM API
  • Default visual themes: Standard, Chrome, and Dark
  • Support for right-to-left languages

For a full list of the new features, see here.

The new version can be downloaded here.

Firebug 1.2 Released

Tagged:  

Firebug, the latest version of the indispensable debugging utility for Firefox, has released version 1.2 of the plugin for Firefox 3.0.

What's New?

  • Selective Panel Enablement: You can now choose to enable any combination of Console (logging), Script (JavaScript debugging) and Net (Network monitoring).
  • Automatic activation of HTML, CSS, and DOM views when Firebug UI is visible/activated (since they require minimal overhead).
  • Ability to suspend/resume Firebug.
  • A new tooltip telling you which tab/page in Firefox has enabled Firebug.

John Resig, creator of jQuery and now lead on the Firebug team, gives an excellent overview on his blog. Resig points out the currently rather global nature of enabling certain Firebug features, the overhead of enabling them (especially Script, which slows down all JavaScript running in Firefox about 25%), and states that all of these issues are targets for the 1.3 release.

Use the Add-ons panel in Firefox to update your plugin or get it now.

The release notes are here.

Syndicate content