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-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-184.108.40.206/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