Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does compiling Java with different versions of JDK and same target and source version guaranties same execution?

Tags:

java

java-8

We are going to update our CI system that creates builds from Java 7 to Java 8. Later we would like to migrate projects to Java 8 one by one. Of course, we would like to be able to create bugfix releases for older versions that still use Java 7.

Can we be sure that no issues will appear if we transfer building same source, target version and source version from JDK 7 to JDK 8? We did testing on development machines without any issues.

Before that, we are also gradually updating deployment servers from JRE 7 to JRE 8.

Note that we would like to keep single Java JDK installation on CI system, it gets complicated otherwise.

To make it more clear: I am more interested in same execution outcome, not so much about the same bytecode. Same bytecode would just confirm the first (running on same JRE), if I understand that correctly.

We will not use Java 8 features in code until we have all deployments running on Java 8. So compatibility is not an issue.

like image 714
Anze Rehar Avatar asked Mar 30 '16 14:03

Anze Rehar


3 Answers

Does compiling Java with different versions of JDK and same target and source version produce same bytecode?

There are no guarantees.

Indeed, two .class files created from the same source code using the same compiler with the same options on the same hardware will have the same bytecode, but the files may still be different because of embedded compilation timestamp attributes.

However, if you wanted to compare notionally equivalent bytecode files, this Q&A offers some possible leads:

  • Is there a tool that we could use to compare two Jars at Binary/Byte code level?

I expect that you will find that there are some differences between the bytecodes generated by Java 7 and Java 8 compilers, but that this won't matter.

like image 53
Stephen C Avatar answered Oct 22 '22 18:10

Stephen C


As specified in this blog@https://blogs.oracle.com/darcy/entry/source_target_class_file_version.

Given the same source code, compilers from different releases configured to use the same source and same target (and same bootclasspath!), can still generate different class files because of bug fixes or changes to compiler-internal contracts.

But this problem will not happen to you since you have the same compiler that compiles to different target versions. It just have to emit the specific magic number and byte code compatible for that version. It looks like keeping your version in 1.8 and specifying the target that is either a lower or equal version would work. Looking at this past defect that was addressed, it appears it should work and Oracle works to fix such incompatibilities. http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4175911

Additional resources that should help:

  1. http://www.javaworld.com/article/2077388/core-java/what-version-is-your-java-code.html?page=2
  2. http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html
like image 4
randominstanceOfLivingThing Avatar answered Oct 22 '22 17:10

randominstanceOfLivingThing


yeah!! it actually depends on the code. in some cases it might generate a different bytecode but normally it generate the same bytecode in normal cases.

Different JDKs will have different Java compilers which may emit different bytecodes for the same source code. The javac compiler has evolved over time.

Different major versions of the Java often emit class files that conform to different versions of the classfile specification.

Even if you restrict yourself to one JDK installation, two runs of the compiler on the same source file will produce non-identical .class files.

like image 1
Avinash Nath Avatar answered Oct 22 '22 17:10

Avinash Nath