Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK compliance - a false safety?

I have Java 7 installed but set my projects JDK compliance to Java 6. Unfortunately it turned out that this is not the same as compiling with Java 6.

For example, the interface javax.imageio.stream.ImageInputStream extends Closable in Java 7 which is not the case in Java 6. Using an ImageInputStream where a Closable is expected compiles fine under my settings (Java 7 complying to Java 6) but gives an compile error when using Java 6. Is this supposed to be this way?

like image 476
user953217 Avatar asked Aug 31 '12 07:08

user953217


1 Answers

In order to compile Java code for an older JRE, you need to do two things:

  • Set the compliance level appropriately. This, as explained by dystroy, makes sure the compiler produces bytecode that the old JVM can understand.
  • Use the old Java system libraries. This makes sure you compile against the version of the Java system libraries (java.lang.* , java.net.* etc.) that shipped with the old JRE.

You have covered the first point, but not the second, hence your problem.

How to do this depends on your build environment.

  • In Eclipse, the Java system library to use is set as part of the build path: Go to Project properties / Java Build Path / Libraries, then remove the wrong "JRE System Library" and add the right one using "Add Library...".
  • When compiling with plain javac, you use option -bootclasspath. Example: javac -target 1.5 -bootclasspath jdk1.5.0/lib/rt.jar OldCode.java (from the javac manpage, section "Cross-Compilation Options").
like image 54
sleske Avatar answered Sep 27 '22 17:09

sleske