Wednesday, July 14, 2010

Cross - Compilation

Many a times you want the same call to run on different versions of JRE, say 1.4, 1.5 and 1.6.

Say you have the same code that you want to run on Weblogic version 8, 9 , 10 and 11.

WLS 8.1 uses JRE 1.4

WLS 9x and 10 uses JRE 1.5

WLS 11 onwards uses JRE 1.6

Having three class files / applications for all different versions of JDK for runtime is not the best of the solutions.

Often as we compile a java class (Java-Code) using a different version of JDK say 1.5 and try to run it using a different version of JRE say 1.4 we get this sort of error:

java.lang.UnsupportedClassVersionError: com/Anil (Unsupported major.minor versio
n 50.0)
at java.lang.ClassLoader.defineClass(Ljava/lang/String;[BIILjava/securit
y/ProtectionDomain;)Ljava/lang/Class;(Unknown Source)
at java.security.SecureClassLoader.defineClass(Ljava/lang/String;[BIILja
va/security/CodeSource;)Ljava/lang/Class;(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(Ljava/lang/String;Lsun/misc/Resou
rce;)Ljava/lang/Class;(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(Ljava/net/URLClassLoader;Ljava/lan
g/String;Lsun/misc/Resource;)Ljava/lang/Class;(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run()Ljava/lang/Object;(URLClassLoader.java
:194)
at jrockit.vm.AccessController.do_privileged_exc(Ljava/security/Privileg
edExceptionAction;Ljava/security/AccessControlContext;I)Ljava/lang/Object;(Unkno
wn Source)
at jrockit.vm.AccessController.doPrivileged(Ljava/security/PrivilegedExc
eptionAction;Ljava/security/AccessControlContext;)Ljava/lang/Object;(Unknown Sou
rce)
at java.net.URLClassLoader.findClass(Ljava/lang/String;)Ljava/lang/Class
;(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(Ljava/lang/String;Z)Ljava/lang/Class;
(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Ljava/lang/String;Z)Ljava/
lang/Class;(Launcher.java:274)
at java.lang.ClassLoader.loadClass(Ljava/lang/String;)Ljava/lang/Class;(
Unknown Source)
at java.lang.ClassLoader.loadClassFromNative(II)Ljava/lang/Class;(Unknow
n Source)



The solution is "CROSS COMPILATION".

The strange thing is the cross compiled file is of the same size as compared to a single JVM compatible class file or the cross compilation will always be of less in size than the compatible class file.

If you wish to write a java code that uses JDK 1.6 and would want it to run on JRE 1.4 crosss compilation is the solution.



However, do make sure you are not using any API that is not supported by the backward release.

The java code should contain all the APIs that can be understood by all the versions of JDK.

For example if you use the new for loop introduced in JDK 1.5, do cross compilation and try to make it run using JRE 1.4 it will be successful however, it would fail if the coded method "String.isEmpty()" or Class "StringBuilder" was not a part of the then release of JDK 1.4. Sample Test-Code.



You can also make use of the third party tools like "RetroTranslator".

http://retrotranslator.sourceforge.net/

Let me know if you liked the article.

No comments:

Post a Comment