Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA Compilation : compile with new JVM version and run on older version

Tags:

java

Here is a small explanation of my problem.

I have an application which was compiled with java 1.5. This application is installed on 2000 pc (blockboxes) installed at customer premisses. The jar of my application is often updated to add new feature and fixes, but for technical reasons, it is not very easy to update the java version, so I must keep using Java 1.5 on those existing machines.

Few months ago, I got a request for a new requirement for this application. To fulfil this task I have added the usage of Hazelcast in this application.

My problem if the following: - Hazelacast jar file needs Java 1.6 or above, so I must compile my application with 1.6 - The new functionality using hazelcast will only be activated on demand by settings a new parameter. This means that it will not be used in the 2000 already installed blackboxes. - All new blackboxes will be installed with Jave 1.6 or above to be able to use the Hazealcast functionality.

My problem is that I want to have a unique source code and unique version of my application for old blackboses using Java 1.5 and new blackboxes using 1.6 or above.

In the beginning, my idea was to always compile with version 1.5 and make sure that the new functionality would only be activated in blockboses using java 1.6 or above. This option is not working, because when I compile with 1.5, the compiler complains that Hazelcast jar file needs 1.6 :(

The second option would be to compile with 1.6, but then I cannot be sure that my application will still work properly on all blackboxes using 1.5. :(

I'm would like to know if someone here would know how to solve this kind of problem?

Just let me know if my explanation is not clear ;)

Thanks in advance for your help.

like image 290
Ewmi Avatar asked Oct 19 '22 07:10

Ewmi


2 Answers

JVM is Backward compatible.You can run almost all code from Java 1 on Java 8.

So the best way is to use the option two. Compile it with 1.6 on some testing machines. And if it works( which most probably will) you don't have to make much change to the application .

like image 122
gRaWEty Avatar answered Oct 22 '22 01:10

gRaWEty


You can compile your code to Java 1.5 bytecode using JDK 1.6, just take care of the following:

  • -source=1.5 and -target=1.5 compiler options

  • bootclasspath should point to rt.jar from JRE 1.5

See this post for more info: http://www.draconianoverlord.com/2014/04/01/jdk-compatibility.html

The post also recommends simply building your application with older JDK if possible. You need to figure out a different build process and exclude incompatible libraries from the classpath. If you use Maven, consider having two pom.xml files, with an optional parent file.

like image 34
Karol S Avatar answered Oct 22 '22 00:10

Karol S