Run different java based on command line parameter
The examples you show do precisely what you are looking for. So, though a command line parameter will not do the trick, an environment variable will. And you can specify 'one-time' environment variables on the command-line, preceding the command.
Most, if not all, Java based software will honour the JAVA_HOME
environment variable, and run the JVM located underneath that directory. Maven certainly does: have a look at less $(which mvn)
and see just how much effort it puts in getting this right.
Note that - in the specific case of Maven - if your goal is to compile Java sources for an older JVM version, then there is no need to switch JVMs. Instead, specify the source and target Java versions in your POM.
Related videos on Youtube
RJo
Updated on September 18, 2022Comments
-
RJo almost 2 years
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
andupdate-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'srt.jar
, which caused incompatibility between different JREs. The solution was to add the java 6'srt.jar
to the classpath:JAVA_HOME=$JAVA6_HOME MAVEN_OPTS="-Xbootclasspath/a:$JAVA6_HOME/jre/lib/rt.jar" mvn clean install
-
zwets almost 11 yearsDon't the examples you show do precisely what you want? Java based software will normally honour the JAVA_HOME environment variable.
-
RJo almost 11 yearsHowever, I still don't understand why the compiling process does not take the
JAVA_HOME
variable in account while determining the compilation classpath. In this case thert.jar
did come from the JDK7 even if theJAVA_HOME
was set. Similarly, the-Xbootclasspath
option did not work by itself. Instead, settingJAVA_HOME
to point to jdk6 was needed.
-
-
RJo almost 11 yearsYes, this is what I thought. The project's
source
andtarget
are both set to1.6
, but still it shows compilation error on my JDK7. This is because Java 7 has an added methodCommonDataSource#getParentLogger()
. The build runs fine if I first runupdate-java-alternatives --set java-6-sun
and thenmvn install
, but not withupdate-java-alternatives --set java-7-oracle
. Not with normalmvn install
norJAVA_HOME=... mvn install
. Need to further investigate this.. -
zwets almost 11 yearsWouldn't it be best to fix the source code so it is forward compatible? I.e. implement
getParentLogger()
on whichever class implements theCommonDataSource
interface? BTW: are you sure the source and target versions are picked up in the compile phase? -
RJo almost 11 yearsThe problem was resolved by adding
-Xbootclasspath/a:$JAVA6_HOME/jre/lib/rt.jar
toMAVEN_OPTS
when compiling it while simultaneously using thesource
andtarget? parameters of the
maven-compiler-plugin. The dependency comes straight from the JRE's own
rt.jar` -
RJo almost 11 yearsFor the source to be compatible with java versions 6 and 7, I would need to add the method without the
@Override
annotation since it would then break with anyone compiling it with jdk 6. -
RJo almost 11 yearsThis is indeed the correct answer to the question. However, it seems that my problem was different. Thank you in any case!