use different java version to run two programs

17,084

Solution 1

There's no point in having them both in $PATH because only one will get used. You could symlink one to a different name -- e.g. java6 -- I've never tried this w/ java and not sure if it would work.

The best way to do this would be to install one of them (presumably 1.6) in a location like /opt/java6, leaving 1.7 as the default. Then when you want to use 6:

export PATH=/opt/java6/bin:$PATH

And start it from the command line. You could also put all that together in a script. Don't try to run Cassandra from the same shell after that unless you remove that from $PATH (easy way to check is echo $PATH).

To automate this for one specific application:

#!/bin/sh

export PATH=/opt/java6/bin:$PATH
exec /path/to/application

You can then put that somewhere in the regular $PATH (e.g., /usr/local/bin), make sure it is executable (chmod 755 whatever.sh) and start the application that way. It will then not affect $PATH in the process which launches it.

Solution 2

This works both launching from a terminal emulator and from a desktop icon launcher for a program I have that will not run on with my default openJRE-11. Using Debian-10 buster with XFCE

/bin/sh -c 'JAVA_HOME=/usr/lib/jvm/java-8-oracle/ PATH=$JAVA_HOME/bin/:$PATH /home/username/myapplication' Might also try: JAVA_HOME=/usr/lib/jvm/java-8-oracle/ PATH=$JAVA_HOME/bin/:$PATH /home/username/myapplication

Also you could make an alias for each java version so that something short like j8o = /bin/sh -c 'JAVA_HOME=/usr/lib/jvm/java-8-oracle/ PATH=$JAVA_HOME/bin/:$PATH and the final everyday command would be j8o ./myapplication

To change the default java update-alternatives --config java then follow the prompt. The same can be used for setting the default for javac (the bytecode compiler) if needed. May need sudo to change the default.

Share:
17,084

Related videos on Youtube

Irene
Author by

Irene

Updated on September 18, 2022

Comments

  • Irene
    Irene over 1 year

    I installed two JAVA JREs on my new CentOS since Cassandra needs java7u25 or later while iReport needs to work with 1.6.

    Now how do I launch each program from command line telling each program which version to use?

    Do I have to change the /etc/profile file? If so how?

    • user3822006
      user3822006 over 9 years
      I don't have Java installed...but have you tried (if you have both versions of Java installed) you can probably run them like Java6 path/to/iReport and Java7 path/to/Cassandra or specifying a full path to Java executable /path/to/Java
  • goldilocks
    goldilocks over 9 years
    I don't see why you'd have to open the third shell. You just want to change the path in the second one (via export) then start iReport. Using two separate tabs in a GUI terminal will work for this. I've edited in what I meant by "putting this in a script", if you want to do that.
  • Max Power
    Max Power over 4 years
    I know the default java can be changed with update-alternatives --config java but you would need to change this every time you switch between applications. The basic "java" command just symlinks to one of the versions in /usr/libs/jvm... if you were given java source rather than pre-compiled bytecode you also may need to change javac which compiles java.