Java 8, now what?

What you'll need to know to start your Java 8 migration process today

There was recently a thread on the London Java Community mailing list about when people should think about adopting Java 8. Lambdas, an improved collections library, new date and time support, and a host of under-the-hood tweaks, add up to a lot of compelling reasons for people to upgrade. There’s still a lot of confusion over when and how to accomplish it, though, so here’s a helpful guide.

When will Java 8 be released?

The GA (General Availability) release of Oracle’s JRE and JDK, which is probably the JVM that you’re using, released March 18th. It may take other JVM vendors a while to release their implementations if you aren’t using an OpenJDK or the Oracle JDK.

So I should just upgrade on release date, right?

That would be a very brave move to make. A huge amount of resources go into testing Java 8 and ensuring that things will work out of the box on the day of release. However, the massive ecosystem of Java libraries means that not everything can be tested to destruction in time. It’s incredibly likely that there will be outstanding bugs upon release. You should expect update releases a month or two after GA, they’ll solve the major problems.

It’s also important to think about what libraries or frameworks your application depends on. If you’re just writing plain old Java then an existing codebase is likely to work fine. If, on the other hand, you depend on a library or framework that tries to do something clever then you may run into problems.

A common issue that frameworks have is that they do some kind of bytecode manipulation or weaving. This means that they depend on a library to deal with the bytecode. Many libraries depend on ASM, which as only just released version 5.0—the version that works on Java 8. It will still take time for other libraries to upgrade to this version of ASM.

Until that happens a lot of libraries that you’re expecting to run on may be broken. Some incompatibilities may even be harder to detect—if you run your existing codebase on a Java 8 version it may run fine. You will only encounter the bytecode manipulation issue if you use a Java 8 language feature. In general, you may not notice any issues running a Java 7 application on Java 8 until you add your first lambda (and get a runtime failure from your app server).

Thus, my best advice is to add a couple Java 8 language features before you test your application.

Does that mean I have to wait years before I upgrade?

Not necessarily. Many libraries have already started the upgrade process. For example, Spring 4 comes ready for Java 8 out of the box. The key is determining when your application can be migrated to Java 8. An easy way of bootstrapping this process is configuring your Continuous Integration server to run builds on both Java 7 and Java 8 at the same time. I’ve previously blogged about how to do this here [2].

If you are blocked by dependent libraries or app servers not supporting Java 8 yet then don’t panic. It’s likely that they will offer support quite soon and filing bugs can help prioritize Java 8 support.

I’m still stuck on Java 6—should I upgrade?

Yes. I would be a bit more careful around testing your software stack, and it might be mean upgrading more libraries. In reality, sticking to Java 6 in 2014 should be considered technical debt in the sense that you’re unable to make use of newer libraries, such as streams, that improve your developer experience. The move to Java 8 is part of paying down this technical debt and improving productivity.

How can you mitigate the risk of an early adoption? What about an incremental migration?

Adopting can be broken down into a series of steps, which helps mitigate risk:

  1. Test and build your code with Java 8 (see link below).
  2. Try and get the libraries that you depend upon up to the latest version, so that they either support Java 8 or so that it’s easy to move to a version that does support Java 8.
  3. Get your team up to scratch with understanding the Java 8 language features. This doesn’t need to take ages but being able to write idiomatic stream code is a real win and something you can start working on today. I’ve put some links below with book/blog information.
  4. Start using the backport for Java 7 of the new date and time library – see http://www.threeten.org/ for details. NB: this isn’t perfectly in sync with the Java 8 version but will help reduce the impedance mismatch when porting.
  5. File bugs which block your application from working based upon the testing you do in (1). This will help get your blockers prioritized.
  6. Don’t try and port all of your code in one go. It will be highly tempting to rewrite all your legacy loops using lambdas. This is a nice thing to do but adds comparatively little business value compared to using these features in new code and can delay migration and other development.

Is there any way to develop on Java 8 and deploy to Java 7?

There’s a cool project called retrolambda; it transforms your Java 8 bytecode to run under Java 7. This would let you develop to Java 8 and deploy to Java 7. Whilst cool, this probably isn’t a great route for many people due to the lack of support and testing. I’m not saying that the retrolambda guys aren’t doing a great job! If you’re in a large corporate environment I would be hesitant to take the risk of their bytecode rewriting implementation when there are so many complexities these days between the libraries and the JVM.

What about developing on Java 7 and deploying to Java 8?

That should work fine. This actually lets you take advantage of the performance improvements expected in Java 8 without having to upgrade or modify your codebase. It also lets you test out Java 8 in the wild with reduced library incompatibilities.

This is ok as part of an incremental migration, but I would still recommend taking the plunge and moving your codebase to Java 8 though mainly for the productivity and readability wins.

Where can I find out more information?

I’ve tried to make the migration process as easy as possible by writing a lot:

  • If you are interested in doing a build of your software under Java 7 and 8 then I’ve already blogged about it here.
  • If you want to know more about the date and time library in Java 8 then there is a subscription free article I’ve written here.
  • If you or your team want to learn about lambdas then I’ve written a book on the topic.
tags: , , ,

Get the O’Reilly Programming Newsletter

Weekly insight from industry insiders. Plus exclusive content and offers.