183

I followed all the instructions stated at this question, but am encountering some problems with the last part of it.

I actually have version 6.22 of java and would like to update to version 6.30.

So after moving the extracted directory java-6-oracle into /usr/lib/jvm I do not know what to do, since the script that is pointed out in the answer above updates from java 5 to java 6.

For sake of clearness here is output if I do an ls in dir /usr/lib/jvm:

$ ls -l /usr/lib/jvm
total 8 
lrwxrwxrwx  1 root root   14 2011-07-12 15:18 default-java -> java-6-openjdk
lrwxrwxrwx  1 root root   14 2011-07-12 12:19 java-1.6.0-openjdk -> java-6-openjdk 
drwxr-xr-x 10 root root 4096 2012-04-12 12:06 java-6.31-oracle 
drwxr-xr-x  7 root root 4096 2012-02-24 14:43 java-6-openjdk

What should I do now?


ADDED PART

Under the suggestion of @fossfreedom I ran the script anyway and actually it updated the java version.

In fact if I run command java -version output will be the following:

$ java -version
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) Server VM (build 20.6-b01, mixed mode)

There is still a problem, if Irun javac -version it gives me the old version installed:

$ javac -version
javac 1.6.0_22

And if I use the tester at this link it will tell me that version is

Java Version 1.6.0_22 from Sun Microsystems Inc.

What's going wrong?

It seems that Java Runtime Environment has updated, but Java Compiler and Java plugin for browser have not.

How can I update them?


OTHER ADDED PART

sudo update-alternatives --config java will return following output

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                      Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6.31-oracle/bin/java     1062      auto mode
  1            /usr/lib/jvm/java-6-openjdk/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-6.31-oracle/bin/java     1062      manual mode

Press enter to keep the current choice[*], or type selection number:

these makes sense with the fact that JRE is correctly updated to version 6.31, issues are on Java Compiler and Java browser plugin.

Any ideas?

10
  • @fossfreedom I haven't tried it cause I thought it wasn't a good idea being it explicitely written for uploading from Java 5 to 6, should I anyway?
    – Matteo
    Apr 13, 2012 at 12:01
  • 1
    hmmm - possibly you may be confusing that the webupd8 script is 0.5b? That is the version of the script - it doesnt refer to the java version. Give it a try. Lets see a screenshot of what the window looks like when you run the script.
    – fossfreedom
    Apr 13, 2012 at 12:13
  • @fossfreedom yeah that totally confused me ;D I ran the script! check the edits in question!thks a lot for helping me..
    – Matteo
    Apr 13, 2012 at 12:16
  • how very strange ... never encountered that. Anyway - look at the answer just below - you should be able to sudo update-alternatives as per the example.
    – fossfreedom
    Apr 13, 2012 at 12:20
  • @fossfreedom updated again question!
    – Matteo
    Apr 13, 2012 at 12:29

7 Answers 7

146

Re your first question:

possibly you may be confusing that the webupd8 script is 0.5b. That is the version of the script - it doesnt refer to the java version.

Further to the setting of the javac version.

I suspect you need to explicitly give the path of the javac compiler

i.e.

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/java-6.31-oracle/bin/javac" 1

followed by:

sudo update-alternatives --config javac

With regards to setting up the java chrome plugin.

The master question:

How do I install Oracle JDK 6?

includes this information - since your folder structure is slightly different your link command should be:

ln -s /usr/lib/jvm/java-6.31-oracle/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins/
1
  • On its own this just changes the compiler - see the answer below for update-java-alternatives which handles switching all of the java-related alternatives, which is 82 on my system.
    – Greg
    May 10, 2014 at 10:16
95

See this; run

sudo  update-java-alternatives --list

to list off all the Java installations on a machine by name and directory, and then run

sudo  update-java-alternatives --set [JDK/JRE name e.g. java-8-oracle]

to choose which JRE/JDK to use.

If you want to use different JDKs/JREs for each Java task, you can run update-alternatives to configure one java executable at a time; you can run

sudo  update-alternatives --config java[Tab]

to see the Java commands that can be configured (java, javac, javah, javaws, etc). And then

sudo  update-alternatives --config [javac|java|javadoc|etc.]

will associate that Java task/command to a particular JDK/JRE.

You may also need to set JAVA_HOME for some applications: from this answer you can use

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")

for JREs, or

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:jre/bin/java::")

for JDKs.

1
  • The first command needs no sudo :)
    – Al.G.
    Aug 20, 2021 at 7:24
27

I think you should take a look at the update-java-alternatives command from the java-common package and the *.jinfo files used by it. With that you should be able to switch completely between java installations (regardless of JDK, JRE, ...).

1
  • 2
    I tried the update-java-alternatives and then java -version was correct, but the link in $JAVA_HOME was still wrong... I changed that one manually, but I wonder whether there is something missing in the update-java-alternatives in that regard. Apr 16, 2016 at 5:09
21

On ubuntu/linux you can switch java version using

update-alternatives --config java

But before, you need install the version.

You can use this script (./install-java.sh) to install multiple JVMs

#!/bin/sh
(
lookforJdks=$PWD
echo "Directory: $lookforJdks"
jdks=`test -e ./javac || find $lookforJdks -type d -iname '*jdk1.*' 2> /dev/null`
#set -e
echo 'which jdk do you want to choose? looking for jdks. This might take a while'
echo "$jdks" | awk '{printf("%5d : %s\n", NR,$0)}'
read choose
test -e ./javac || cd `echo "$jdks" | tr '\n' ',' | cut -d',' -f $choose`/bin
for e in appletviewer extcheck idlj jar jarsigner java javac javadoc javah javap jconsole \
 jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic \
 schemagen serialver wsgen wsimport xjc jvisualvm jmc; do sudo update-alternatives \
 --install /usr/bin/$e $e $(readlink -f ./$e) 100; done
)

echo "RUN update-alternatives --config java"

Put this script in folder where has unpacked the JVM(s), and run:

/opt/install-java.sh

Next use:

update-alternatives --config java
1
  • Why not use the software updater to install the various JDK/JREs?
    – TheGeeko61
    Aug 26 at 23:56
2

If you are going to manualy set alternatives for javac javaws javadoc or any other java related commands, you might want to make them slaves of the java altenative.

sudo update-alternatives --install "/usr/bin/java" "java" "/opt/jdk1.8.0_74/bin/java" 0 --slave "/usr/bin/javadoc" "javadoc" "/opt/jdk1.8.0_74/bin/javadoc"


update-alternatives: updating alternative /opt/jdk1.8.0_74/bin/java because link group java has changed slave links


:/opt/jdk1.8.0_74$ sudo update-alternatives --display java

java - auto mode link best version is /opt/jdk1.8.0_74/bin/java link currently points to /opt/jdk1.8.0_74/bin/java link java is /usr/bin/java slave javadoc is /usr/bin/javadoc /opt/jdk1.8.0_74/bin/java - priority 0 slave javadoc: /opt/jdk1.8.0_74/bin/javadoc

Change /opt/jdk1.8.0_74/ with the dir where your jdk is installed.

1
  • This is what is needed.. a uniform way to switch Java versions... but there is a dance between update-alternatives and update-java-alternatives... that I don't quite see amongst all these answers going (like an octopus) in many different directions all at once leading to no general solution... we've come from versions 5 & 6 to 17 & 18. Most of these answers won't oblige. This answer is a good start.... combine with jinfo details and the dance between update-alternatives and update-java-alternatives.
    – TheGeeko61
    Aug 27 at 0:07
0

If you need, for example, to automatically switch to Java 8 (compiler 1.8), in an one-liner, for example for a script or continuous integration test suite, you can run

sudo update-java-alternatives -s $(sudo update-java-alternatives -l | grep 8 | cut -d " " -f1) || echo '.'

It will automatically fetch any java 8 version available and set it using the command update-java-alternatives.

The || echo '.' at the end just ensures the command returns success, because strangely update-java-alternatives by default returns error (1). You may skip it if you don't need this for a test suite.

If you want to switch to any other version just replace the 8 by the version you want in the previous command.

-1

Open /etc/environment in any your text editor and add the following line:

JAVA_HOME="usr/lib/jvm/open-jdk 

- Java path could be diffrent.
Use source to load the variables, by running this command:

source /etc/environment

Then check the variable, by running this command:

echo $JAVA_HOME

With this path set, run this command:

sudo  update-java-alternatives --list

Choose your default Java Version.

1
  • Sourcing /etc/environment also corrupts your PATH. ???
    – TheGeeko61
    Aug 26 at 23:49

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