0

I try to write my code compatible with older versions of java, so it will work everywhere.

on the other hand there is very powerful tools in the new version - java 8, and I want use them.

So I'm forced to choose between compatibility or richest code.

And I'm wondering if by any chance I can write some methods in java 8, and somehow prevent the compiler of older version to ignore these methods, so my class is compatible "partially" with older version.

Thanks.

chmouel kalifa
  • 129
  • 2
  • 11

3 Answers3

1

You can write two classes and use some toll like ant, maven or gradle to chose which file use for compiling with concrete Java version.

VDanyliuk
  • 1,049
  • 8
  • 23
  • OK, it's an idea, so there is not any annotation or something like it to say to the compiler to ignore this method. am I correct? – chmouel kalifa Dec 08 '15 at 17:28
0

You can set the java compiler to compile against an older jdk (ie jdk 1.5) even if you use jdk 1.8. see javac source and target options

Community
  • 1
  • 1
chrisl08
  • 1,658
  • 1
  • 15
  • 24
0

I think the short and easy answer is no. See this thread: Can Java 8 code be compiled to run on Java 7 jvm?

You can use the java reflection api to check if methods exist in the jvm the code runs on. This allows you to make your code fail-safe even when a method or class is unavailable in the jvm. Doing this is very cumbersome however and I'm pretty sure it's not what your're looking for.

Community
  • 1
  • 1
Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18