Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java 7 SDK features in Java 6

Tags:

java

java-7

nio2

I am interested in using some of the NIO2 features in the Java 7 SDK if available (specifically, the file system watchers), however I do not want to compile my classes for Java 7 and exclude Java 6 runtimes. Mostly because I want retain compatibility with Mac OS X, and also because I don’t want to force my users to upgrade.

Is this possible? What is the best way to do it? Any links or examples?

Here are some ways I can imagine: compiling a class file with a different compiler and loading it dynamically based on the Java version? Or maybe using reflection? Or maybe there’s just a compiler setting for Java 7 to generate Java 6-compatible classes?

I am looking for a solution that doesn’t turn into an ugly mess :), so ideally I can write two implementations of an interface, one using the new features and one without, and then select one dynamically instead of having to do reflective calls all over the place.

like image 240
Laurens Holst Avatar asked Sep 29 '11 14:09

Laurens Holst


2 Answers

Just build with -target 1.6 and organize your code so you can catch the ClassNotFoundExceptions and NoClassDefFoundErrors cleanly around the modules that use 1.7. Maybe load them with a separate class-loader for example.

like image 105
user207421 Avatar answered Oct 11 '22 11:10

user207421


You can build for java 1.6 easily as toolkit has pointed out. However, you need to make sure that you don't accidentally access any methods which don't exist in java 6. This will cause a runtime exception in your production code.

If you're using maven, you can use the maven-enforcer-plugin which makes sure that no java 1.7 classes or method calls sneak into your code built for 1.6.

An example would be the change from java 1.4 to 1.5. I was building with 1.5 with a target of 1.4, and I accidentally used:

new BigDecimal(5);

This compiled fine, and ran fine for me. But because the client was still using 1.4, it failed. Because this constructor doesn't exist in 1.4. It was introduced in 1.5.

Another solution would be to build a couple of jars, one with the new nio stuff, one with the old stuff, and detect at installation time whether or not the user was running java 1.7. If so, add the jar that contains the appropriate implementation.

like image 38
Matthew Farwell Avatar answered Oct 11 '22 13:10

Matthew Farwell