Run different java based on command line parameter

14,745

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.

Share:
14,745

Related videos on Youtube

RJo
Author by

RJo

Updated on September 18, 2022

Comments

  • RJo
    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 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
    
    • zwets
      zwets almost 11 years
      Don't the examples you show do precisely what you want? Java based software will normally honour the JAVA_HOME environment variable.
    • RJo
      RJo almost 11 years
      However, 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 the rt.jar did come from the JDK7 even if the JAVA_HOME was set. Similarly, the -Xbootclasspath option did not work by itself. Instead, setting JAVA_HOME to point to jdk6 was needed.
  • RJo
    RJo almost 11 years
    Yes, this is what I thought. The project's source and target are both set to 1.6, but still it shows compilation error on my JDK7. This is because Java 7 has an added method CommonDataSource#getParentLogger(). The build runs fine if I first run update-java-alternatives --set java-6-sun and then mvn install, but not with update-java-alternatives --set java-7-oracle. Not with normal mvn install nor JAVA_HOME=... mvn install. Need to further investigate this..
  • zwets
    zwets almost 11 years
    Wouldn't it be best to fix the source code so it is forward compatible? I.e. implement getParentLogger() on whichever class implements the CommonDataSource interface? BTW: are you sure the source and target versions are picked up in the compile phase?
  • RJo
    RJo almost 11 years
    The problem was resolved by adding -Xbootclasspath/a:$JAVA6_HOME/jre/lib/rt.jar to MAVEN_OPTS when compiling it while simultaneously using the source and target? parameters of the maven-compiler-plugin. The dependency comes straight from the JRE's own rt.jar`
  • RJo
    RJo almost 11 years
    For 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
    RJo almost 11 years
    This is indeed the correct answer to the question. However, it seems that my problem was different. Thank you in any case!