![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCGE8DJE_cYdAN17F3qFl9ydUZuvEJxmCLi51NbCR5B8jFc5vub0o_eDrQ8meKEuUMoYg7tNEg5HgSfJj9QUHk191muAiTmQrQYe9lTQRP-sdzFW0eriyTKFs0BfwBuORhEFEsDD3aYtKY/s1600/Write+Java+with+JDK+1.5+features+and+run+on+JRE+1.4+www.digizol.com.png)
![Java](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQd1zYuYhdKGm5elCoXJWbGBHB2dERAvq7q8F9MVWK-9X6H2KHrwcS2LnO8x3m5gXLS_IX_poblKvb8nz0Xw1wMdAJLb1lcMvj5k_qkhkgKer4y-1s6LfsN2718pgnow6p_UoR2K9VPSxQ/s320/java-logo.gif)
Compile 1.5 codes for 1.4 JVM
Java compiler provides an option to specify the target JVM of the generated classes like 1.5, 1.4, and 1.3 as follows.
public class MyClass {
public static void main(String args[]) {
System.out.println("Main Method");
}
}
Compile with...
javac -source 1.4 -target 1.4 MyClass.java
Even if your JDK is of version 1.5, above command will compile your class so that it can run on a JRE 1.4. But the above option only works if your class does not use any of the new features in Java 1.5 (like auto boxing, enums etc).
Check the following class. Compiling it with "target" option will result in a compile time error.
public class MyClass {
public static void main(String args[]) {
Integer count = 1;
System.out.println("Main Method");
}
}
Compile with...
javac -source 1.4 -target 1.4 MyClass.java
The compiler error is shown because auto boxing is not supported in 1.4.
This means that we can not use 1.5 features if we are going to run the programs on an older version. So this is not a real solution for our issue.
Can not run/deploy code with new features on 1.4 JVM?
![Java retroweaver logo](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQAYzKG8TOXLZ65721RKXERl0LQkEiC2BHn7fZqkQTotBE2lZwPlf_bR-YVvK5a-0rwPdEIhRd0RBPTrpV030xvHIw29fMXwtLf4CcJm7kOnTADS8RrmFFWlgnSwPG8xHsUoZp8BqTZB6k/s320/retroweaver-logo.jpg)
Retroweaver site says;
Retroweaver supports most 1.5 features while running on 1.4.
- generics
- extended for loops
- static imports
- autoboxing/unboxing
- varargs
- enumerations
- annotations
Also this has a good documentation (even though it's not up to date. Don't blame them; they are doing it for free).
This tool can convert .class files of 1.5 version to another older JRE version. Also it has the capability of converting a complete .jar file.
0 Response to "Write Java with JDK 1.5 features and run on JRE 1.4"
Post a Comment