Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have installed both OpenJDK 6 and 7. When I run "java somefile" from the command line, OpenJDK 6 is invoked. I do not want to change this default behavior. What command can I use to run my non-default OpenJDK 7 installation instead?

(I am used to running "python somefile" to invoke the default Python, "python2.7 somefile" to use Python 2.7 specifically and "python3 somefile" to use Python 3 specifically.)

share|improve this question
up vote 4 down vote accepted

You could bind an alias to the specific java versions. Just add the following lines to your $HOME/.bashrc:

alias java6='/path/to/java6/bin/java'
alias java7='/path/to/java7/bin/java'

Same for javac and you should be fine ;-)

share|improve this answer
the paths to the different java executables look like: /usr/lib/jvm/java-7-openjdk-amd64/bin/java – ImaginaryRobots Sep 5 '12 at 15:25
Java usually expects the $JAVA_HOME environment variable to be set. You should use something like JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64 – Stephen Ostermiller May 12 at 19:51

You can select java version using update-alternatives command.

Run below command in terminal.

sudo update-alternatives --config java

And type selection number that you want to use.

Like this:

selecting java version

Caution: The above description is the procedures for changing a default version. It is not an answer of this question, but it leaves for reference.

share|improve this answer
+1 for the screenshot – anatoly techtonik Aug 13 '15 at 4:57
Much better to use update-java-alternatives instead. That way, you update all the java related commands. – Boon Jul 22 at 14:45

Your Answer


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.