Java

Netbeans 6.5 Released

Tagged:  

In the tradition of "Release early, release often", the Netbeans team has released version 6.5, continuing the rapid release cycle the project set with 5.5, 6.0, and 6.1. There many new features, most notably support for Groovy /Grails and PHP. Here's a short list of the other "new and notables":

  • A new "Compile and Deploy on Save" feature for Java applications.
  • Support for the Nimbus look and feel in the Swing GUI builder (Matisse)
  • Big improvements in the JavaScript support, particularly in the area of debugging.
  • Support for Ruby on Rails 2.1 (JRuby 1.1.4 is bundled)
  • Improved SQL support, including SQL history and editor auto-completion.

There is also an early access release of Python support in Netbeans that is available as a separate download.

Get Netbeans 6.5 here.

I Want My Type Information (Back)

Tagged:  

As Neal Gafter explained a while ago, there is a backdoor in Java's generic type erasure:

getClass().getGenericSuperclass()

Google Guice makes use of this backdoor to enable the construction of a generic type literal (or Super Type Token): its TypeLiteral class. Now (the soon to be released) Guice 2.0 goes a step further: it can inject a TypeLiteral into your class, thereby reifying your generic types (well, sort of). All the details of the changes to com.google.inject.InjectorImpl are here.

As Neal Gafter also explained, java.lang.reflect.Type really should be retrofitted. Until then, Guice offers you a little help.

Google Guice 2.0 in Beta

Tagged:  

For those following dependency injection frameworks in Java, Google Guice has released its beta snapshot (stamped 2008-10-16) for Guice 2.0.

What's coming in Guice 2.0?

  • A tree-like structure for Injectors, i.e., an Injector can have children that inherit all of its bindings.
  • An introspection API: like reflection but for Guice bindings.
  • The ability to inject members of sets and maps using Multibinder and MapBinder respectively.
  • Private modules.
  • Pluggable type converters that convert constant string bindings to other types, i.e., Dates, URLs, etc.
  • And more...

The official word is now that Guice 2.0 will be released in Fall 2008. (The Id Software release schedule: it'll be done when it's done!)

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.

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.

Get cross-site JSON via HTTP with GWT and the GWT Designer by Instantiations

Tagged:  

The GWT is a great development tool for designing Ajax applications. Since JSON is a great way to exchange data from the server to the client, in many cases, it is good to find a tutorial combining the two. Well, over at the Giant Flying Saucer blog they have put together a nice tutorial about cross-domain JSON and GWT. The tutorial is written for users of the GWT Designer, but even if you don't use that tool this tutorial has some good information.

Below is an excerpt from the tutorial.

If your a web developer then chances are pretty good you've already heard of and possibly used JSON. In a nutshell, JSON is a lightweight way to exchange data. In the second tutorial we did our communications to and from the server via RPC (Remote Procedure Calls). This time we'll modify the code to use JSON instead and call a third-party server (we will simulate this with a Python web server on the same computer).
Assuming you've got everything ready go ahead and open the project now. One of the first things the GWT JSON tutorial shows is how simple the JSON format is:

XML:

[
  {

    "symbol": "BA",
    "price": 87.86,
    "change": -0.41
  },
  {

    "symbol": "KO",
    "price": 62.79,
    "change": 0.49
  },
  {

    "symbol": "JNJ",
    "price": 67.64,
    "change": 0.05
  }
]

You can see JSON is just name/value pairs and human readable. Its simpler than XML and less verbose (less eyeball noise). Keep in mind though if you do need XML support that GWT offers that as well. Today though we are focusing purely on JSON.
So how do we actually get the JSON from the server to the client? Well, fortunately GWT provides everything we need in the form of the HTTP client classes. There are three items in particular we'll use from there:
1. RequestBuilder and calling the "sendRequest" method

2. RequestCallback which (remember the callback we had in the second tutorial?) will call "onResponseReceived" on a successful callback or "onError" if something goes wrong.
- Note: Toward the end of this tutorial we will replace the RequestBuilder code.

You can read the full tutorial here.

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.

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.

CGLIB 2.2 Becomes Final

Tagged:  

From the "snuck by me department": CGLIB 2.2, which had hovered in beta for a seemingly endless period of time, became final in May of 2008. The brief release notes explain that it contains a patch for a race condition in MethodProxy as well as an upgrade to ASM 3.1. For those who aren't familiar with CGLIB, it is a very lightweight Java library originally created as a faster alternative to java.lang.reflect.Proxy. It has seen use in many well-known projects such as Hibernate, Spring, and more recently Google Guice.

Syndicate content