Monday, March 30, 2009

Switching between different Java versions, Java 5 instead of Java 6

Since like many of you I am developing multiple projects simultaneously and have a very heterogeneous environment on my Windows machine, I always looked for a simple way to switch between the latest Java version and the previous one, which I still needed for some older projects.
Once installed, the latest JRE/JDK (currently, Java 6) become the JVM of choice for any app in Windows OS and it was quite tricky for me to force an app to be executed under Java 5.

I saw many posts on the Internet claiming they solved the issue by manually
editing Windows registry and/or replacing soem files in Windows/system32 directory - never wanted to do that.

This post is probably the closest to what I wanted to do, however the code from this post did not work 'as is' in my batch files.

Looking at java.exe command options in Java 6 I found an amazing feature:
-version:

This is what the command line java says about it:
-version:
require the specified version to run

Sounds just what I needed! So I run it like this:
>java -version:1.5.0_17 -version

And voila! - here is what says my Java 6 now:
java version "1.5.0_17"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_17-b04)
Java HotSpot(TM) Client VM (build 1.5.0_17-b04, mixed mode)


Of course if I run it with just -version (no ":" and no alternative JRE/JDK), it keeps saying:
> java -version
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

No comments: