【原文地址】http://maciek.hubpages.com/hub/Ubuntu-Install-Sun-Java-Alternatives
There are variety of ways you can have Java (JDK) installed on your Ubuntu. You can stick with the defaultOpenJDK (openjdk-6-jdk package in Ubuntu repository) or install Sun (it’sOracle now actually) Java (sun-java6-jdk package) from Ubuntu’s multiverse repository using package manager (apt-get). Usually they are couple of revisions behind official Sun JDKreleases, but most of the time it is enough.
Sometimes however you want to (or need to) install the latest version of Java. Downloading and unpacking it is quite easy. However making it the default Java installation in your system is harder. You can of course manually change all or part of the symbolic links pointing to java commands, but it just doesn’t feel like the proper way to do it. Why not use theupdate-java-alternatives command that makes changes system-wide and allows to easily change the default distribution at any time.
Unfortunately update-java-alternatives knows nothing about your manually downloaded JDK, so below are steps that adds new alternative to the registry ofJDKs available.
Notice that all the commands presented here should be pasted as one line in your terminal (even if they are split by browser).
Install Java step by step
- Download JDK from Sun/Oracle. Choose bin version (not rpm). The file would be in form of jdk-version-platform.bin (in my case it was: jdk-6u25-linux-x64.bin )
- Add execution rights to the downloaded file
$ chmod u+x jdk-6u25-linux-x64.bin
- Execute script. It will unpack its content to current directory (press Enter when asked)
$ ./jdk-6u25-linux-x64.bin
Now you should have directory i.e. jdk1.6.0_25/ with JDK files in it. - Move unpacked JDK to final destination e.g. /opt/java/
$ sudo mv jdk1.6.0_25/ /opt/java/
- Create symbolic link to that directory that will represent this JDK in general regardless current version. It will ease the future updates of JDK. I can any name. I have chosen sun-java6-manual
$ cd /opt/java/
$ sudo ln -s jdk1.6.0_25 sun-java6-manual
- Now create another symbolic link in /usr/lib/jvm pointing to previous one.
$ cd /usr/lib/jvm/
$ sudo ln -s /opt/java/sun-java6-manual
- I assume that you have already installed Sun JDK from Ubuntu repositories before. If this is the case then there should be .java-6-sun.jinfo file in /usr/lib/jvm/directory. We will copy that file and modify it. If there is no such file you can download it to current directory with this command:
$ sudo apt-get -d install sun-java6-bin && find /var/cache/apt/archives/ -iname sun-java6-bin* -exec ar -p '{}' data.tar.gz \; | tar -zxO ./usr/lib/jvm/.java-6-sun.jinfo | sudo tee .java-6-sun.jinfo
You can delete it afterwards. The reason I am not using .java-6-openjdk.jinfo file that probably is present in your Ubuntu installation is that it is slightly different that Sun’s one (some browser plugin entries are missing).
Copy file and replace path entries that they point to created link in /usr/lib/jvm/
$ sudo sed s/java-6-sun/sun-java6-manual/ .java-6-sun.jinfo | sudo tee .sun-java6-manual.jinfo
- Now edit the beginning of file so it looks like below:
$ sudo vim .sun-java6-manual.jinfo
name=sun-java6-manual
alias=sun-java6-manual
priority=30
Choose whatever priority you like. - Now we have to register new alternatives in update-alternatives mechanism.
$ cat .sun-java6-manual.jinfo | grep -E '^(jre|jdk)' | awk '{print "/usr/bin/" $2 " " $2 " " $3 " 30 \n"}' | xargs -t -n4 sudo update-alternatives --verbose --install
- Do the same with plugins
$ sudo update-alternatives --verbose --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /usr/lib/jvm/sun-java6-manual/jre/lib/amd64/libnpjp2.so 30
$ sudo update-alternatives --verbose --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so /usr/lib/jvm/sun-java6-manual/jre/lib/amd64/libnpjp2.so 30
- Now we are ready to switch default Java to the new one.
$ update-java-alternatives -l
It should list available java alternatives. It may look like:
java-6-openjdk 1061 /usr/lib/jvm/java-6-openjdk
java-6-sun 63 /usr/lib/jvm/java-6-sun
sun-java6-manual 1500 /usr/lib/jvm/sun-java6-manual
- Set chosen option with:
$ sudo update-java-alternatives -s sun-java6-manual
- Now confirm that java command is the new installed one
$ java -version
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.0-b11, mixed mode)
Updating JDK
When you want to update JDK, download and unpack somewhere and change link/opt/java/sun-java6-manual to point to it.