Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile a .java with support for older versions of Java?

I want to compile my .java's (several) into one .jar that are compatible with at least Java 1.6 and newer, preferably Java 1.5 and newer versions of Java. (I have Java 1.7.0_5)

like image 231
Primm Avatar asked Jul 06 '12 15:07

Primm


People also ask

How do I compile older versions of Java?

Compiling for an older execution platform If you need to compile Java to run on an older Java platforms, the simplest approach is to install a JDK for the oldest version you need to support, and use that JDK's compiler in your builds. You can also compile with a newer Java compiler, but there are complicated.

Can older JRE versions run Java program compiled with newer JDK versions?

As answered already you are mostly safe and most products and 3rd party libraries will simply work. However there do exist very rare cases where binary incompatibilities (ones where the class file compiled using older JDK will fail to run in the newer JVM) were introduced between JDK versions.


2 Answers

Yes, you can set the version of compiler at compile time. And compile your java code into old versions of java.

From Oracle article : http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html

Cross-Compilation Example

Here we use javac to compile code that will run on a 1.4 VM.

% javac -target 1.4 -bootclasspath jdk1.4.2/lib/classes.zip \              -extdirs "" OldCode.java 

You might also need following parameter to set denote the version of your code.

-source release 

-Specifies the version of source code accepted.

like image 122
gtiwari333 Avatar answered Sep 29 '22 03:09

gtiwari333


As of JDK 9, javac support a new option for cross-compiling

javac --release N ... 

which is equivalent to

javac -source N -target N –bootclasspath rtN.jar 
like image 33
Joe Darcy Avatar answered Sep 29 '22 04:09

Joe Darcy