168

Java 8 introduces important new language features such as lambda expressions.

Are these changes in the language accompanied by such significant changes in the compiled bytecode that would prevent it from being run on a Java 7 virtual machine without using some retrotranslator?

1

5 Answers 5

151

No, using 1.8 features in your source code requires you to target a 1.8 VM. I just tried the new Java 8 release and tried compiling with -target 1.7 -source 1.8, and the compiler refuses:

$ javac Test -source 1.8 -target 1.7
javac: source release 1.8 requires target release 1.8
4
  • 4
    No, I don't think it will. Java has a small share of the desktop market, but keeps that little share in a pretty tight grip. But it does hamper the adoption of new versions and features. I will not be able to use Java 8 features in the code I write for quite some time, since I want to avoid people having to upgrade their local Java installation.
    – JesperE
    Aug 15, 2014 at 10:05
  • Why? "Yes" would imply the Java 8 can be compiled to run on a Java 7 VM, which is incorrect according to the Java 8 compiler.
    – JesperE
    Oct 28, 2014 at 17:22
  • 5
    Now I see: Your "No" answers the question's headline, not the question's body.
    – Abdull
    Oct 28, 2014 at 17:37
  • But then why do we call JAVA Write Once Run Anywhere if we cannot run code written in java 8 on a java 7 machine?
    – mnagdev
    Jun 8, 2022 at 10:57
60

Default methods require such changes to the bytecode and the JVM that they would have been impossible to do on Java 7. The bytecode verifier of Java 7 and below will reject interfaces with method bodies (except for the static initializer method). Trying to emulate default methods with static methods on the caller side would not produce the same results, because default methods can be overridden in subclasses. Retrolambda has limited support for backporting default methods, but it can never be fully backported because it truly requires new JVM features.

Lambdas could run on Java 7 as-is, if the necessary API classes just would exist there. The invokedynamic instruction exists on Java 7, but it would have been possible to implement lambdas so that it generates the lambda classes at compile time (early JDK 8 builds did it that way) in which case it would work on any Java version. (Oracle decided to use invokedynamic for lambdas for future proofing; maybe one day JVM will have first-class functions, so then invokedynamic can be changed to use them instead of generating a class for every lambda, thus improving performance.) What Retrolambda does is that it processes all those invokedynamic instructions and replaces them with anonymous classes; the same as what Java 8 does at runtime when a lamdba invokedynamic is called the first time.

Repeating Annotations is just syntactic sugar. They are bytecode compatible with previous versions. In Java 7 you would just need to implement yourself the helper methods (e.g. getAnnotationsByType) which hide the implementation detail of a container annotation which contains the repeated annotations.

AFAIK, Type Annotations only exist at compile time, so they should not require bytecode changes, so just changing the bytecode version number of the Java 8-compiled classes should be enough to make them work on Java 7.

Method parameter names exist in the bytecode with Java 7, so that's also compatible. You can get access to them by reading the bytecode of the method and looking at the local variable names in the method's debug information. For example the Spring Framework does exactly that to implement @PathVariable, so there is probably a library method which you could call. Because abstract interface methods don't have a method body, that debug information doesn't exist for interface methods in Java 7, and AFAIK neither on Java 8.

The other new features are mostly new APIs, improvements to HotSpot and tooling. Some of the new APIs are available as 3rd party libraries (e.g. ThreeTen-Backport and streamsupport).

Summa summarum, default methods require new JVM features but the other language features don't. If you want to use them, you'll need to compile the code in Java 8 and then transform the bytecode with Retrolambda to Java 5/6/7 format. At minimum the bytecode version needs to be changed, and javac disallows -source 1.8 -target 1.7 so a retrotranslator is required.

1
33

As far as I know none of these changes in JDK 8 required the addition of new bytecodes. Part of the lambda instrumentation is being done using invokeDynamic (which already exist in JDK 7). So, from the JVM instruction set standpoint, nothing should make the codebase incompatible. There are, though, a lot of API associated and compiler improvements that would could make the code from JDK 8 difficult to compile/run under previous JDKs (but I have not tried this).

Maybe the following reference material can help somehow to enrich the understanding of how the changes related to lambda are being instrumented.

These explain in detail how things are instrumented under hood. Perhaps you can find the answer to your questions there.

12
  • 7
    No new bytecodes, but new structures. The verifier will puke. Jun 12, 2014 at 3:38
  • 12
    A good example is Interfaces. They can now contain methods. The Java7 verifier is not equipped to handle this. All of the old bytecodes are used, but in a new way. Oct 28, 2014 at 21:53
  • 1
    I wonder how can scala compiler with so many language features achieve a target jvm release of even jdk5.
    – Marinos An
    Apr 5, 2016 at 12:44
  • 2
    @MarinosAn What do you mean exactly? MI with traits that contain concrete methods, e.g. class C extends A with B, is implemented with normal interfaces A and B and companion classes A$class and B$class. class C simply forwards the methods to the static companion classes. Self-types are not enforced at all, lambdas are transpiled on compile-time to abstract inner classes, so is a new D with A with B expression. Pattern-matching is a bunch of if-else structures. Non-local returns? try-catch mechanism from the lambda. Anything left? (Interestingly, my scalac says 1.6 is the default)
    – Adowrath
    Apr 8, 2017 at 16:50
  • 1
    Of course, self-types etc. are encoded in special class attributes and annotations so that scalac can use and enforce the rules when using already compiled classes.
    – Adowrath
    Apr 8, 2017 at 16:52
11

If you are willing to use a "retrotranslator" try Esko Luontola's excellent Retrolambda: https://github.com/orfjackal/retrolambda

0
-6

You can do -source 1.7 -target 1.7 then it will compile. But it won't compile if you have java 8 specific features like lambdas

1
  • The question explicitly posits use of the new language features, so -source 1.7 won't fly.
    – toolforger
    Jan 28, 2019 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.