N.B.: Remember - Java JDK has many more executables that you can similarly install as above. java, javac, javaws are probably the most frequently required. This answer lists the other executables available.

Run

sudo update-alternatives --config java

You will see output similar to the one below - choose the number of jdk1.8.0 - for example 3 in this list (unless you have have never installed Java installed in your computer in which case a sentence saying "There is nothing to configure" will appear):

N.B.: You can link the plugin (libnpjp2.so) to /usr/lib/firefox/plugins/ for a system-wide installation (/usr/lib/firefox-addons/plugins from 15.04 onwards). For Ubuntu 13.10, the path to the plugin directory is /usr/lib/firefox/browser/plugins/.

Depending on your configuration, you might need to update the apparmor profile for Firefox (or other browsers) in /etc/apparmor.d/abstractions/ubuntu-browsers.d/java:

N.B.: Before someone screams this is against the Oracle redistribution license - the PPA does not actually have Java in the personal repository. Instead, the PPA directly downloads from Oracle and installs it.

The Script way

If you're on a fresh installation of Ubuntu with no previous Java installations, this script automates the process outlined above if you don't want to type all that into a console. Remember, you still need to download Java from Oracle's website -- Oracle's links are not wget friendly.

Before using this make sure that this script is in the same directory as the .tar.gz file extension that you downloaded and there are no files that start with jdk-7 in the same folder. If there are, please move them out of the folder temporarily. Remember to make the script executable (chmod +x <script's file>).

Is necessary to do all the same process to update it?
–
LucioMay 24 '13 at 15:50

2

This worked successfully for me installing the 32-bit JDK on Ubuntu 12.04LTS, except that I was getting bash: /usr/bin/java: No such file or directory when I tried to run java -version. apt-get install libc6-i386 fixed the problem.
–
gregmacJul 31 '13 at 20:57

I suggest the use /usr/local/* in place of /usr/* for any manual installation like here. That way what you install manually wont clutter up with stuff managed by package manager and make manual installation more manageable
–
FlintMay 1 '14 at 5:59

1

I strongly recommend the manual way out of this. It is the most reliable. The PPA doesn't work anymore since Oracle Java is licensed. And the script above assumes you are using the compiler and the runtime and that version on it is out of date.
–
JohnMerlinoJul 18 '14 at 18:44

After running sudo update-alternatives --config java, I get the following output:There is only one alternative in link group java: /usr/lib/jvm/java-6-openjdk/jre/bin/java Nothing to configure. What am I doing wrong?
–
ShoanMar 18 '12 at 4:56

7

great answer, thanks! Wonder why Oracle can't spend the time to make packages for this....
–
bbqchickenrobotMar 26 '12 at 22:11

1

@Shoan, in that case, go to step "if only one alternative is shown then remember the number 0"
–
henriqueMar 27 '12 at 1:57

3

Thank you for this answer, Oracles instructions are rubbish for a linux noob, this made things alot easier!
–
David BarkerMay 9 '12 at 9:33

Thanks! Now I can finally use the Sun JVM! EDIT: crud, this breaks Minecraft on my 64-bit system. (Strange, considering that the specifically ask you to use Sun's JVM.) The launcher opens OK, but when I try to launch it, I get the Black Screen, even after deleting my ~/.minecraft/bin/ folder, and then updating. Running from the terminal, it gives me some gibberish about wrong ELF type, and that it's probably an architecture mismatch. (The official Java verifier on the Oracle site says everything's fine, though...)
–
JamesTheAwesomeDudeNov 11 '12 at 21:24

Extract the downloaded Oracle Java JDK archive in your home folder - a new folder called "jdk1.7.0_03" (for Java JDK7 update 3) should be created. Rename it to "java-7-oracle" and move it to /usr/lib/jvm using the following commands:

I think this script would work for Gnome (tools) but requires a lot of additional installs as for Kubuntu machine (eg. missing gksudo on Kubuntu).
–
Marcin GilJan 12 '12 at 9:36

good and relatively painless solution on Precise, just installed jdk1.7.0_03 on it
–
prusswanFeb 24 '12 at 6:55

I just used this method to put jdk1.7.0_03 on Precise beta 2, as well. It worked, although an error was reported (in Zenity, evidently); the error doesn't appear to have affected the result.
–
KelleyApr 20 '12 at 16:28

I am wondering why most people are tempted to use the first answer, which is quite complex and requires a lot of manual work, like downloading the installer on your machine and moving it to the server. Your answer is simple and effective!!! TX
–
sorinJul 25 '12 at 15:02

4

@SorinSbarnea Some people don't like to use third party repositories because they aren't always updated or online for that matter.
–
umop aplsdnAug 27 '12 at 21:30

On Ubuntu server, I had to use "sudo apt-get install python-software-properties" to get the add-apt-repository command.
–
Mark ButlerMar 13 '13 at 7:11

@SorinSbarnea because the repository is generally well out of date
–
dstarhApr 22 '13 at 15:31

@sorin some of us are working with multiple java versions, and the first answer kinda reminds us of verifying which version is currently active
–
prusswanJul 10 '14 at 2:31

Works great, except the final command issues an error (though everything else works, even in Firefox/Chrome). This seems to be from the default OpenJDK6 install with Oneiric."update-alternatives: error: no alternatives for -javaplugin.so." In the hidden .java.1.6.0.jinfo file there seems to be an alternative pointing to -javaplugin.so. Are we supposed to make a 3rd alternative point to libnpjp2.so with it perhaps?
–
NicholiOct 25 '11 at 23:13

I suggest using one of the installers from the webupd8team ppa in this way:

1) Enter these 2 commands in the Terminal to add this PPA and update your packages list:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update

2) Enter one (or more) of these commands to install the proprietary Java(s) you require:

sudo apt-get install oracle-java6-installer

sudo apt-get install oracle-java7-installer

sudo apt-get install oracle-java8-installer

3) When the respective Java installer script is loaded and then it downloads and installs the proprietary Java packages, you can also enter the following command to check the result of the installation:

NB: These packages provide Oracle Java JDK, which includes Java JDK, JRE and the Java browser plugin. And the webupd8team/java PPA contains only these installers, which simply connect to Oracle Java download site and install the latest JDK.

This will build the official sun-java6-* packages and make them available in the software-center. If you also want oracle-java7-* packages, run the script like this:

sudo ./oab-java.sh -7

Now you can install the packages with your preferred package manager.

The explanation

When Oracle changed the distribution license for Java, Linux distributions weren't allowed to update the official packages anymore. Github user rraptorr took the Debian script that was used to build the packages until then, and modified it to work with the updated downloads from Oracle. He now provides packging scripts for SUN Java6 and Oracle Java7.

The oab.java.sh script is just a wrapper that automatically downloads the current version, creates the packages and adds them to a local repository.

Further notes

Packages created and installed using this method, will not receive updates automatically - only when you rerun the script. You can circumvent this by creating a cronjob that runs this script on a regular basis. The savest way to do this is using anacron by putting the script in /etc/cron.daily.

I've had so many issues with this script -_-
–
qodeninjaSep 25 '12 at 5:04

@qodeninja, like what? I've never had an issue with this script. Executing the 3 line above will definately work on every Ubuntu machine, if it is not broken.
–
mniessSep 26 '12 at 16:28

I have a new linode instance of ubuntu 10.04 and I was trying to install jre 7 with this script. The script would hang for far too long because of missing dependencies. Then I had to stop the script and install the dependencies myself with apt-get. Even after I had everything installed the script still failed.
–
qodeninjaSep 26 '12 at 18:20

The script really doesn't do anything special. Sounds like something is wrong with your box. I have been (and am) using this from 8.04 to 12.04 and never had problems. I just tried this on my 10.04 server which is pretty much vanilla, and it works as well. As it should, because this script doesn't do more than use the old debian build-scripts that packagers used to provide sun-java* for debian (and ubuntu).
–
mniessSep 27 '12 at 22:27

Nice solution. This should be upvoted higher up. The installation time may take longer than 5 minutes depending on your internet speed (the download is ~70MB). As for setting the default Java machine, I prefer: sudo update-alternatives --config java.
–
landroniOct 5 '14 at 14:15

please try not to link out to external stuff as an answer - give the most relevant parts in your answer. The reason is that external links may disappear in the future - and therefore others will not be able to refer to the answer in the future. Thanks.
–
fossfreedom♦Aug 17 '11 at 19:27

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
–
Kevin BowenJun 21 '13 at 8:05

Please do not post duplicate answers on different questions. It just creates more work for the moderators. If the questions are so similar that the same answer works on each, then the later of the two is likely a duplicate and should be flagged as such.
–
RolandiXor♦Nov 22 '13 at 4:28

update-alternatives: using /usr/lib/jvm/jre1.7.0_05/bin/java to
provide /usr/bin/java/ (java) in auto mode. update-alternatives:
error: unable to make /usr/bin/java/.dpkg-tmp a symlink to
/etc/alternatives/java: No such file or directory