Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java 10 compiled classes run on 9? [duplicate]

Tags:

java

java-10

If I understand correctly the binary form of Java 10 is 100% identical to Java 9 so it shouldn't make any problems.

Is my assumption correct?

like image 437
Mordechai Avatar asked Mar 20 '18 03:03

Mordechai


People also ask

Can Java 8 compiled code run on Java 7?

Java SE 8 is binary-compatible with Java SE 7 except for the incompatibilities listed below. Except for the noted incompatibilities, class files built with the Java SE 7 compiler will run correctly in Java SE 8. Class files built with the Java SE 8 compiler will not run on earlier releases of Java SE.

Is Java 8 backwards compatible?

Backward Compatibility Java versions are expected to be binary backwards-compatible. For example, JDK 8 can run code compiled by JDK 7 or JDK 6. It is common to see applications leverage this backwards compatibility by using components built by different Java version.

Can Java be fully compiled?

It is technically possible to compile Java down to native code ahead-of-time and run the resulting binary. It is also possible to interpret the Java code directly. To summarize, depending on the execution environment, bytecode can be: compiled ahead of time and executed as native code (similar to most C++ compilers)

How many .Java files are created for a class with many files?

The java compiler produces one file per class, including for inner classes (anonymous or not). They will always be a .


1 Answers

This has nothing to do with Java 10; the rules are the same as they've always been.

In every version of Java, the classfile version number has been incremented. You can run older classfiles on a newer Java runtime (you can run files compiled twenty years ago under Java 1.0 on Java 10!), but it has never been true that you can run newer classfiles on an older runtime. This hasn't changed with Java 10.

However, you can compile classes such that they run on older versions, if you don't use newer features introduced since that version. This used to be accomplished by the -source and -target switches:

// Run this on Java 8
javac -source 6 -target 6 Foo.java

and you'll get classfiles that will run on Java 6 and later. (The source version cannot be greater than the target version.) This was recently replaced with the more comprehensive --release option:

javac --release 9

which implies not only -source 9 and -target 9, but the compiler will also prevent you from using JDK functionality introduced after 9.

like image 61
Brian Goetz Avatar answered Sep 25 '22 12:09

Brian Goetz