Sunday, October 06, 2013

Lambda Expressions on Java SE 6


I attended a talk at JavaOne 2013 on Expression Language 3.0. It was a great talk by Ed Burns and Kin-man Chung about EL. EL 3.0 has been modular for a while, and can operate independently of the EE platform. I knew that, but this was a siren call reminder of that fact. During the talk, I asked a number of questions and for clarifications. One interesting clarification was that EL 3.0 was compiled using Java 7. This was following a discussion of the fact that it supports Lambda expressions. Wait... it uses Lambda expressions from Java 8 and is compiled using Java 7. I asked could it be compiled on Java 6. Kin-man Chung, the specification lead, told me that with some minor changes; "it should?" I took that as a challenge to try. I was successful and now I can do Lambda expressions on JDK 6.

When I mentioned that I was using this technique for Java 6 on Twitter, Michael Graciano pointed out that +Adam Bien  had done something similar for Java 1.7. I am not sure if great minds think alike, or fools seldom differ on this one, but his article is a very cool read too.

Technical Details

The easiest way to try it out for yourself is to download the compiled version here: javax.el-3.0.0-custom.jar
If you want to create your own, then follow these easy steps.
  1. Download and install Java CC 6.0
  2. Download Java EL project using NetBeans Team Server on, or using Subversion:
  3. Open the impl directory and modify the build.xml file to point to your Java CC 6.0 directory, for example:
  4. Change the pom.xml to source and target level 1.6
  5. Change the version in the pom.xml so that it is distinguishable from the RI.
  6. Execute a mvn clean
  7. In the impl directory, execute ant. This will compile the parser code for EL.
    Note: This is a very important step!
  8. Modify the org.glassfish.el.test.ELProcessorTest, and remove all of the try with multicatch, and diamond operators.
  9. Execute mvn clean install.

If you were successful, you will have a version of EL 3.0 that will run on Java SE 6.

You can test your code using some of the examples found in the references below, or using the sample code snippet that uses some of the code from the references.



Popular Posts