4

Is it possible to develop using newer Java (e.g 9, 10, 11 etc.) versions, and have that Java be compiled down to some older version - such as Java 8?

We would like to use the new features of newer Java versions, but our code needs to run on a Java 8 platform.

If it matters: we are building with Maven.


Phrasing it in Javascript fashion:

Does there exist a Babel Polyfill equivalence for Java?

Tormod Haugene
  • 3,538
  • 2
  • 29
  • 47
  • 3
    Unless I'm missing something you can define which version a class is compiled to. However, you cannot use features not supported by a lower version, i.e. you can't use lambdas when compiling for Java 7 or below. – Thomas Mar 28 '19 at 14:39
  • 2
    if *needs to run on a Java 8 platform* - that is problem you are _really_ having – Eugene Mar 28 '19 at 14:44

2 Answers2

6

Not necessarily. If you have code that uses packages/syntactical structures not defined in previous versions, the program will fail to run/compile.

Think of it this way. Before Java 8, there were not lambda expressions. So if you create a lambda expression in Java 8+ and then attempt to compile it in java 7, the compiler will be like: What is a lambda expression?

In short no. However, you can go the other way. Compile code written in a lower version using a higher version... for the most part.

JustAFellowCoder
  • 300
  • 2
  • 11
  • 2
    it is possible that the compiler will not even complain about this, I faced a similar problem, Java 8 compiler was instructed to target Java 6, app testing was ok when running on Java 8, but when running on Java 6 we got execution error as some methods were unavaiblable. So errros were not found until we actually run the program on the target jdk (Java 6) – Jonnathan Q Mar 28 '19 at 14:43
  • 1
    @JonnathanQ thank you for reminding me. Yes it is possible that the compiler will not complain. Such as non-existent methods. – JustAFellowCoder Mar 28 '19 at 14:47
6

No - you cannot use new features on an older JVM; the bytecode is incompatible.

You can absolutely use the newer versions of Java to compile down to a lower version, with some caveats around modules.

If you need the new features of newer Java versions, then it's time to migrate to a new version; other than that, where you are will be fine until you require support from Oracle.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • So, assuming we cannot control the Java version on the platform we are developing for, there is simply no way to use the newer features? I guess this explains some of the popularity for Kotlin. – Tormod Haugene Mar 28 '19 at 14:48
  • 1
    @TormodHaugene: No; again, the bytecode wouldn't be compatible. If you **need** the newer features, then you have to have a JVM which can understand the newer bytecode. Even Kotlin is largely constrained on what new features from the JVM it can support since [it can only target up to Java 8 right now *anyway*](https://kotlinlang.org/docs/reference/faq.html#does-kotlin-only-target-java-6). – Makoto Mar 28 '19 at 14:50
  • Alright. No point looking more into it then. Thanks for clearing that up! – Tormod Haugene Mar 28 '19 at 14:53