Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

(Apologies in advance if my formatting is off, this is my first post, please feel free to correct me).

Normally, when I want to find out what version of java I'm using, I run:

java -version

java version "1.7.0_55"

If I want to change java version (from java 7 to java 6), I can just point the symlink (/usr/bin/java) to the corresponding JREs java binary. (We don't need to worry about the javac compiler or java_ws here, in this instance, btw).

# readlink -f /usr/bin/java

/usr/lib/jvm/java-7-openjdk-i386/jre/bin/java

# ln -s /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java /usr/bin/java

# java -version

java version "1.6.0_31"

This is fine, and as far as I can see, it works.

In Solaris, there is a nice way of doing this through the pkg mediator tool:

myHost# pkg mediator -H java

java system 1.7 system

To change Java from 1.7 to 1.6:

pkg set-mediator -V 1.6 java

Then to confirm the changes

myHost# pkg mediator -H java

java local 1.6 system

I'm wondering does apt have an equivalent? Or is there a more graceful way of switching between java versions?

Edit: This could also apply to python, so I'm not looking for java-specific answers

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The correct way of switching between multiple versions of an installed software is using:

sudo update-alternatives --config <package>

So, for java, you need to execute:

sudo update-alternatives --config java

which should return you the versions installed, which in my case is:

There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-oracle/jre/bin/java          1053      auto mode
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode
  2            /usr/lib/jvm/java-7-oracle/jre/bin/java          1053      manual mode

In order to switch between versions you can enter the selection i.e. 0, 1, 2 in this case, depending upon the version you want to switch to.

However, in case of python, python2 and python3 are not alternatives, so you won't find this option, you will need to create aliases for this.

So, if you want python to refer to python version 2.xx and python3 to refer to python version 3.xx, you can open your ~/.bash_aliases file and enter the following:

alias python=`/usr/bin/python`
alias python3=`/usr/bin/python3`

This file may be empty if you have not created aliases before, which is fine. You may even want the alias the other way around, i.e. python to refer to python3 and python2 to refer to python2, you can tweak the alias as you want.

As you can see, it depends if the versions installed are alternatives of each other- if they are alternatives, you can use update-alternatives, if not, you will have to tweak using aliases or soft-links. There may be better, cleaner solutions, but I am not aware of those.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.