3

Possible Duplicate:
Using Java 7 SDK features in Java 6

I have a Java application (actually it is a Scala application, but I don't think that this changes much), running on JVM 6.

I would like to use some of the new Java 7 APIs like NIO2 if they are available, but fallback to other implementation if they are not available. The goal is to make use of Java 7 APIs if possible while keeping compatibility with Java 6 with the same binaries (jar).

It should be possible by checking with Class.forName("java.nio.file.attribute.BasicFileAttributeView"), but I don't know how if e.g. the java class files have a different version, which would make it impossible to use Java 7 classes in code that is also runnable in Java 6.

Community
  • 1
  • 1
dmeister
  • 34,704
  • 19
  • 73
  • 95
  • If you go with this approach, you will have to use reflection extensively. Otherwise, the code is not going to compile on JDK <7 versions. – Manish Jan 06 '12 at 09:40
  • Sounds messy. Maybe put the Java-7 dependent code in a separate jar file and have two versions of that jar file, deciding which one to actually load (factory pattern) at runtime? – Thilo Jan 06 '12 at 09:51
  • @dogbane I think you are right. – dmeister Jan 06 '12 at 10:54

1 Answers1

3

One way could be - code the application logic using interfaces. Bundle the implementation in two different jars - one containing JDK6 based implementation and the other with JDK7 implementation. At runtime, get the java version using System.getProperty("java.version") and instantiate appropriate implementation (or may be use Strategy pattern to set the appropriate strategy).

Manish
  • 3,913
  • 2
  • 29
  • 45