I have multiple java installations on my machine and need to use different versions all the time. I have found that the currently used JVM can be changed by the commands update-alternatives and update-java-alternatives, which essentially change the link at /etc/alternatives/java etc. to point to the correct JVM installation.
Is it possible to run different versions of java by specifying a command line parameter? E.g., to run maven for example in the following ways:
> JAVA_HOME=/usr/lib/jvm/java-7-oracle/ mvn clean install
> JAVA_HOME=/usr/lib/jvm/java-6-sun/ mvn clean install
EDIT: Above commands work
We can verify that the above commands work by running
> JAVA_HOME=/usr/lib/jvm/java-6-sun mvn -version
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.6.0_32, vendor: Sun Microsystems Inc.
Java home: /usr/lib/jvm/java-6-sun-1.6.0.32/jre
The java version and home are correct in the example.
EDIT: Solution to the problem
The original problem was caused by an interface named CommonDataSource changed in the jre's rt.jar, which caused incompatibility between different JREs. The solution was to add the java 6's rt.jar to the classpath:
JAVA_HOME=$JAVA6_HOME MAVEN_OPTS="-Xbootclasspath/a:$JAVA6_HOME/jre/lib/rt.jar" mvn clean install
JAVA_HOMEvariable in account while determining the compilation classpath. In this case thert.jardid come from the JDK7 even if theJAVA_HOMEwas set. Similarly, the-Xbootclasspathoption did not work by itself. Instead, settingJAVA_HOMEto point to jdk6 was needed. – RJo Sep 24 '13 at 6:16