How can I install Sun/Oracle's proprietary Java JDK 6/7/8 or JRE?

101

Solution 1

There is a similar answer on how to install JRE 7.

Install Java JDK

The manual way

  • Download the 32-bit or 64-bit Linux "compressed binary file" - it has a ".tar.gz" file extension.

  • Uncompress it

    tar -xvf jdk-8-linux-i586.tar.gz (32-bit)

    tar -xvf jdk-8-linux-x64.tar.gz (64-bit)

    The JDK 8 package is extracted into ./jdk1.8.0 directory. N.B.: Check carefully this folder name since Oracle seem to change this occasionally with each update.

  • Now move the JDK 8 directory to /usr/lib

    sudo mkdir -p /usr/lib/jvm
    sudo mv ./jdk1.8.0 /usr/lib/jvm/
    
  • Now run

    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0/bin/java" 1
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0/bin/javac" 1
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.8.0/bin/javaws" 1
    

    This will assign Oracle JDK a priority of 1, which means that installing other JDKs will replace it as the default. Be sure to use a higher priority if you want Oracle JDK to remain the default.

  • Correct the file ownership and the permissions of the executables:

    sudo chmod a+x /usr/bin/java
    sudo chmod a+x /usr/bin/javac
    sudo chmod a+x /usr/bin/javaws
    sudo chown -R root:root /usr/lib/jvm/jdk1.8.0
    

    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):

    $ sudo update-alternatives --config java
    There are 3 choices for the alternative java (providing /usr/bin/java).
    
      Selection    Path                                            Priority   Status
    ------------------------------------------------------------
      0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      auto mode
      1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      manual mode
    * 2            /usr/lib/jvm/jdk1.7.0/bin/java                   1         manual mode
      3            /usr/lib/jvm/jdk1.8.0/bin/java                   1         manual mode
    
    Press enter to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/lib/jvm/jdk1.8.0/bin/java to provide /usr/bin/java (java) in manual mode
    

    Repeat the above for:

    sudo update-alternatives --config javac
    sudo update-alternatives --config javaws
    

Note for NetBeans users!

You need to set the new JDK as default editing the configuration file.


If you want to enable the Mozilla Firefox plugin:

32 bit:
ln -s /usr/lib/jvm/jdk1.8.0/jre/lib/i386/libnpjp2.so ~/.mozilla/plugins/

64 bit:
ln -s /usr/lib/jvm/jdk1.8.0/jre/lib/amd64/libnpjp2.so ~/.mozilla/plugins/

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:

# Replace the two lines:
#  /usr/lib/jvm/java-*-sun-1.*/jre/bin/java{,_vm} cx -> browser_java,
#  /usr/lib/jvm/java-*-sun-1.*/jre/lib/*/libnp*.so cx -> browser_java,
# with those (or adapt to your new jdk folder name)
/usr/lib/jvm/jdk*/jre/bin/java{,_vm} cx -> browser_java,
/usr/lib/jvm/jdk*/jre/lib/*/libnp*.so cx -> browser_java,

Then restart apparmor:

sudo /etc/init.d/apparmor restart

The easy way (Obsolete)

Note: WebUpd8 team's PPA has been discontinued with effective from April 16, 2019. Thus this PPA doesn't have any Java files. More information can be found on PPA's page on Launchpad. Hence this method no longer works and exists because of hostorical reasons.

The easiest way to install the JDK 7 is to do it with the Web Up8 Oracle Java OOS. However, it is believed that this PPA is sometimes out of date. Also note the dangers of using a PPA.

This installs JDK 7 (which includes Java JDK, JRE and the Java browser plugin):

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
# or if you want JDK 8:
# sudo apt-get install oracle-java8-installer
# these commands install Oracle JDK7/8 and set them as default VMs automatically:
# sudo apt-get install oracle-java7-set-default
# sudo apt-get install oracle-java8-set-default

Source

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>).

#!/bin/sh

tar -xvf jdk-7*
sudo mkdir /usr/lib/jvm
sudo mv ./jdk1.7* /usr/lib/jvm/jdk1.7.0
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.7.0/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/lib/jvm/jdk1.7.0/bin/javaws" 1
sudo chmod a+x /usr/bin/java
sudo chmod a+x /usr/bin/javac
sudo chmod a+x /usr/bin/javaws

If you want to install the plugin for Firefox then add this to the end of the script:

mkdir ~/.mozilla/plugins
ln -s /usr/lib/jvm/jdk1.7.0/jre/lib/amd64/libnpjp2.so ~/.mozilla/plugins/
sudo /etc/init.d/apparmor restart

Check if installation was successful

You can check if the installation succeeded with the following command:

java -version

You should see something like

java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b70, mixed mode)

You can check if the JRE Mozilla plugin has been successful by using the official oracle website.


For Java 6: How do I install Oracle JDK 6?

Solution 2

There is a similar answer on how to install JDK 8

Install the JRE

Download the 32-bit or 64-bit Linux "compressed binary file" - it has a ".tar.gz" file extension and uncompress it

tar -xvf jre-7-linux-i586.tar.gz

The JRE 7 package is extracted into ./jre1.7.0 directory. Now move the JRE 7 directory to /usr/lib:

sudo mv ./jre1.7.0* /usr/lib/jvm/jre1.7.0

Afterwards, run the following to get a list of currently installed Java alternatives.

sudo update-alternatives --config java

You will get output as:

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

Selection Path Priority Status
————————————————————
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode

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

Remember the last number and press enter to exit this utility i.e. in this example remember the number 2.

If only one alternative is shown then remember the number 0.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jre1.7.0/bin/java 3

This will add your new JRE 7 installation into the alternatives list i.e. use the remembered number + 1, that is, 3 in the example above. Now configure Java to use the Oracle Java JRE:

sudo update-alternatives --config java

You will see output similar one below - choose the number of jre1.7.0, that is, 3:

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

Selection Path Priority Status
————————————————————
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 auto mode
1 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 manual mode
2 /usr/lib/jvm/java-6-sun/jre/bin/java 63 manual mode
3 /usr/lib/jvm/jre1.7.0/jre/bin/java 3 manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/lib/jvm/jre1.7.0/jre/bin/java to provide /usr/bin/java (java) in manual mode.

N.B.: If there was no previous Java installation then the new JRE will be the default and you will not see the above.

Check the version of you new JRE 7 installation:

java -version

It should produce

java version “1.7.0”
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Client VM (build 21.0-b17, mixed mode)

Install the Firefox/Chrome plugin

In a terminal:

mkdir ~/.mozilla/plugins

Remove the IcedTea plugin, if it has been installed.

sudo apt-get remove icedtea6-plugin

Remove a former version of the Java plugin (may or may not be present):

rm ~/.mozilla/plugins/libnpjp2.so

Now you can install the plugin, by creating a symbolic link (you tell Firefox, where the plugin is located). For 32-bit Java use

ln -s /usr/lib/jvm/jre1.7.0/lib/i386/libnpjp2.so ~/.mozilla/plugins/

For 64-bit Java use

ln -s /usr/lib/jvm/jre1.7.0/lib/amd64/libnpjp2.so ~/.mozilla/plugins/

Confirm that the JRE has been successful by using the official oracle website.

Solution 3

Here is a tested and working solution for installing Oracle JDK 7 and all its files so "javac" and everything else works: How To Install Oracle Java 7 (JDK) In Ubuntu

Here are the commands (just for convenience):

  1. Download the latest Oracle JDK 7 from here.
  2. 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:
cd
sudo mkdir -p /usr/lib/jvm/      #just in case
sudo mv java-7-oracle/ /usr/lib/jvm/

3. Install Update Java package created by Bruce Ingalls (packages available for Ubuntu 11.10, 11.04, 10.10 and 10.04):

sudo add-apt-repository ppa:nilarimogard/webupd8
sudo apt-get update
sudo apt-get install update-java

4. Now run the following command in a terminal to install Oracle Java JDK:

sudo update-java

Select the Java Version that you want to install and set as the default

After a few minutes, Oracle Java JDK should be successfully installed on your Ubuntu machine. You can check out the version by running these commands in a terminal:

java -version
javac -version

NOTICE! This part below here of this answer no longer works due to Java changing how their binaries are released. It has been left as-is for historical reasons.

Update Oracle has released Java 8 (stable). To install it, use the following commands:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

More information @ WebUpd8: Install Oracle Java 8 In Ubuntu Via PPA Repository [JDK8]

Solution 4

NOTICE! This solution no longer works due to Java changing how their binaries are released. As a result, this answer is no longer valid. It has been left as-is for historical reasons.

From http://www.webupd8.org/2012/01/install-oracle-java-jdk-7-in-ubuntu-via.html :

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-jdk7-installer

Solution 5

This is how I installed it in Oneiric just now. It will be a rather lengthy answer, but it worked for me.

Download latest Java SDK 1.7.0 from Oracle. Then extract it to /usr/lib/jvm:

cd /usr/lib/jvm/
sudo tar -xvzf ~/jdk-7-linux-x64.tar.gz
sudo mv jdk1.7.0 java-7-oracle
sudo ln -s java-7-oracle java-1.7.0-oracle

After that I created .java-1.7.0-oracle.jinfo file in /usr/lib/jvm with the following contents:

alias=java-7-oracle
priority=100
section=non-free

jre ControlPanel /usr/lib/jvm/java-7-oracle/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-7-oracle/jre/bin/java
jre java_vm /usr/lib/jvm/java-7-oracle/jre/bin/java_vm
jre javaws /usr/lib/jvm/java-7-oracle/jre/bin/javaws
jre jcontrol /usr/lib/jvm/java-7-oracle/jre/bin/jcontrol
jre keytool /usr/lib/jvm/java-7-oracle/jre/bin/keytool
jre pack200 /usr/lib/jvm/java-7-oracle/jre/bin/pack200
jre policytool /usr/lib/jvm/java-7-oracle/jre/bin/policytool
jre rmid /usr/lib/jvm/java-7-oracle/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/java-7-oracle/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/java-7-oracle/jre/bin/unpack200
jre orbd /usr/lib/jvm/java-7-oracle/jre/bin/orbd
jre servertool /usr/lib/jvm/java-7-oracle/jre/bin/servertool
jre tnameserv /usr/lib/jvm/java-7-oracle/jre/bin/tnameserv
jre jexec /usr/lib/jvm/java-7-oracle/jre/lib/jexec
jdk appletviewer /usr/lib/jvm/java-7-oracle/bin/appletviewer
jdk apt /usr/lib/jvm/java-7-oracle/bin/apt
jdk extcheck /usr/lib/jvm/java-7-oracle/bin/extcheck
jdk idlj /usr/lib/jvm/java-7-oracle/bin/idlj
jdk jar /usr/lib/jvm/java-7-oracle/bin/jar
jdk jarsigner /usr/lib/jvm/java-7-oracle/bin/jarsigner
jdk java-rmi.cgi /usr/lib/jvm/java-7-oracle/bin/java-rmi.cgi
jdk javac /usr/lib/jvm/java-7-oracle/bin/javac
jdk javadoc /usr/lib/jvm/java-7-oracle/bin/javadoc
jdk javah /usr/lib/jvm/java-7-oracle/bin/javah
jdk javap /usr/lib/jvm/java-7-oracle/bin/javap
jdk jconsole /usr/lib/jvm/java-7-oracle/bin/jconsole
jdk jdb /usr/lib/jvm/java-7-oracle/bin/jdb
jdk jhat /usr/lib/jvm/java-7-oracle/bin/jhat
jdk jinfo /usr/lib/jvm/java-7-oracle/bin/jinfo
jdk jmap /usr/lib/jvm/java-7-oracle/bin/jmap
jdk jps /usr/lib/jvm/java-7-oracle/bin/jps
jdk jrunscript /usr/lib/jvm/java-7-oracle/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/java-7-oracle/bin/jsadebugd
jdk jstack /usr/lib/jvm/java-7-oracle/bin/jstack
jdk jstat /usr/lib/jvm/java-7-oracle/bin/jstat
jdk jstatd /usr/lib/jvm/java-7-oracle/bin/jstatd
jdk native2ascii /usr/lib/jvm/java-7-oracle/bin/native2ascii
jdk rmic /usr/lib/jvm/java-7-oracle/bin/rmic
jdk schemagen /usr/lib/jvm/java-7-oracle/bin/schemagen
jdk serialver /usr/lib/jvm/java-7-oracle/bin/serialver
jdk wsgen /usr/lib/jvm/java-7-oracle/bin/wsgen
jdk wsimport /usr/lib/jvm/java-7-oracle/bin/wsimport
jdk xjc /usr/lib/jvm/java-7-oracle/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so

Then you need to tell update-alternatives about all the new stuff:

$ sudo -sH
Password:
# update-alternatives --quiet --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so 100
# update-alternatives --quiet --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /usr/lib/jvm/java-7-oracle/jre/lib/amd64/libnpjp2.so 100
# update-alternatives --quiet --install /usr/bin/appletviewer appletviewer /usr/lib/jvm/java-7-oracle/bin/appletviewer 100 --slave /usr/share/man/man1/appletviewer.1 appletviewer.1 /usr/lib/jvm/java-7-oracle/man/man1/appletviewer.1
# update-alternatives --quiet --install /usr/bin/apt apt /usr/lib/jvm/java-7-oracle/bin/apt 100 --slave /usr/share/man/man1/apt.1 apt.1 /usr/lib/jvm/java-7-oracle/man/man1/apt.1
# update-alternatives --quiet --install /usr/bin/extcheck extcheck /usr/lib/jvm/java-7-oracle/bin/extcheck 100 --slave /usr/share/man/man1/extcheck.1 extcheck.1 /usr/lib/jvm/java-7-oracle/man/man1/extcheck.1
# update-alternatives --quiet --install /usr/bin/idlj idlj /usr/lib/jvm/java-7-oracle/bin/idlj 100 --slave /usr/share/man/man1/idlj.1 idlj.1 /usr/lib/jvm/java-7-oracle/man/man1/idlj.1
# update-alternatives --quiet --install /usr/bin/jar jar /usr/lib/jvm/java-7-oracle/bin/jar 100 --slave /usr/share/man/man1/jar.1 jar.1 /usr/lib/jvm/java-7-oracle/man/man1/jar.1
# update-alternatives --quiet --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/java-7-oracle/bin/jarsigner 100 --slave /usr/share/man/man1/jarsigner.1 jarsigner.1 /usr/lib/jvm/java-7-oracle/man/man1/jarsigner.1
# update-alternatives --quiet --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 100 --slave /usr/share/man/man1/javac.1 javac.1 /usr/lib/jvm/java-7-oracle/man/man1/javac.1
# update-alternatives --quiet --install /usr/bin/javadoc javadoc /usr/lib/jvm/java-7-oracle/bin/javadoc 100 --slave /usr/share/man/man1/javadoc.1 javadoc.1 /usr/lib/jvm/java-7-oracle/man/man1/javadoc.1
# update-alternatives --quiet --install /usr/bin/javah javah /usr/lib/jvm/java-7-oracle/bin/javah 100 --slave /usr/share/man/man1/javah.1 javah.1 /usr/lib/jvm/java-7-oracle/man/man1/javah.1
# update-alternatives --quiet --install /usr/bin/javap javap /usr/lib/jvm/java-7-oracle/bin/javap 100 --slave /usr/share/man/man1/javap.1 javap.1 /usr/lib/jvm/java-7-oracle/man/man1/javap.1
# update-alternatives --quiet --install /usr/bin/jconsole jconsole /usr/lib/jvm/java-7-oracle/bin/jconsole 100 --slave /usr/share/man/man1/jconsole.1 jconsole.1 /usr/lib/jvm/java-7-oracle/man/man1/jconsole.1
# update-alternatives --quiet --install /usr/bin/jdb jdb /usr/lib/jvm/java-7-oracle/bin/jdb 100 --slave /usr/share/man/man1/jdb.1 jdb.1 /usr/lib/jvm/java-7-oracle/man/man1/jdb.1
# update-alternatives --quiet --install /usr/bin/jhat jhat /usr/lib/jvm/java-7-oracle/bin/jhat 100 --slave /usr/share/man/man1/jhat.1 jhat.1 /usr/lib/jvm/java-7-oracle/man/man1/jhat.1
# update-alternatives --quiet --install /usr/bin/jinfo jinfo /usr/lib/jvm/java-7-oracle/bin/jinfo 100 --slave /usr/share/man/man1/jinfo.1 jinfo.1 /usr/lib/jvm/java-7-oracle/man/man1/jinfo.1
# update-alternatives --quiet --install /usr/bin/jmap jmap /usr/lib/jvm/java-7-oracle/bin/jmap 100 --slave /usr/share/man/man1/jmap.1 jmap.1 /usr/lib/jvm/java-7-oracle/man/man1/jmap.1
# update-alternatives --quiet --install /usr/bin/jps jps /usr/lib/jvm/java-7-oracle/bin/jps 100 --slave /usr/share/man/man1/jps.1 jps.1 /usr/lib/jvm/java-7-oracle/man/man1/jps.1
# update-alternatives --quiet --install /usr/bin/jrunscript jrunscript /usr/lib/jvm/java-7-oracle/bin/jrunscript 100 --slave /usr/share/man/man1/jrunscript.1 jrunscript.1 /usr/lib/jvm/java-7-oracle/man/man1/jrunscript.1
# update-alternatives --quiet --install /usr/bin/jsadebugd jsadebugd /usr/lib/jvm/java-7-oracle/bin/jsadebugd 100 --slave /usr/share/man/man1/jsadebugd.1 jsadebugd.1 /usr/lib/jvm/java-7-oracle/man/man1/jsadebugd.1
# update-alternatives --quiet --install /usr/bin/jstack jstack /usr/lib/jvm/java-7-oracle/bin/jstack 100 --slave /usr/share/man/man1/jstack.1 jstack.1 /usr/lib/jvm/java-7-oracle/man/man1/jstack.1
# update-alternatives --quiet --install /usr/bin/jstat jstat /usr/lib/jvm/java-7-oracle/bin/jstat 100 --slave /usr/share/man/man1/jstat.1 jstat.1 /usr/lib/jvm/java-7-oracle/man/man1/jstat.1
# update-alternatives --quiet --install /usr/bin/jstatd jstatd /usr/lib/jvm/java-7-oracle/bin/jstatd 100 --slave /usr/share/man/man1/jstatd.1 jstatd.1 /usr/lib/jvm/java-7-oracle/man/man1/jstatd.1
# update-alternatives --quiet --install /usr/bin/native2ascii native2ascii /usr/lib/jvm/java-7-oracle/bin/native2ascii 100 --slave /usr/share/man/man1/native2ascii.1 native2ascii.1 /usr/lib/jvm/java-7-oracle/man/man1/native2ascii.1
# update-alternatives --quiet --install /usr/bin/rmic rmic /usr/lib/jvm/java-7-oracle/bin/rmic 100 --slave /usr/share/man/man1/rmic.1 rmic.1 /usr/lib/jvm/java-7-oracle/man/man1/rmic.1
# update-alternatives --quiet --install /usr/bin/schemagen schemagen /usr/lib/jvm/java-7-oracle/bin/schemagen 100 --slave /usr/share/man/man1/schemagen.1 schemagen.1 /usr/lib/jvm/java-7-oracle/man/man1/schemagen.1
# update-alternatives --quiet --install /usr/bin/serialver serialver /usr/lib/jvm/java-7-oracle/bin/serialver 100 --slave /usr/share/man/man1/serialver.1 serialver.1 /usr/lib/jvm/java-7-oracle/man/man1/serialver.1
# update-alternatives --quiet --install /usr/bin/wsgen wsgen /usr/lib/jvm/java-7-oracle/bin/wsgen 100 --slave /usr/share/man/man1/wsgen.1 wsgen.1 /usr/lib/jvm/java-7-oracle/man/man1/wsgen.1
# update-alternatives --quiet --install /usr/bin/wsimport wsimport /usr/lib/jvm/java-7-oracle/bin/wsimport 100 --slave /usr/share/man/man1/wsimport.1 wsimport.1 /usr/lib/jvm/java-7-oracle/man/man1/wsimport.1
# update-alternatives --quiet --install /usr/bin/xjc xjc /usr/lib/jvm/java-7-oracle/bin/xjc 100 --slave /usr/share/man/man1/xjc.1 xjc.1 /usr/lib/jvm/java-7-oracle/man/man1/xjc.1
# update-alternatives --quiet --install /usr/bin/java-rmi.cgi java-rmi.cgi /usr/lib/jvm/java-7-oracle/bin/java-rmi.cgi 100
# update-alternatives --quiet --install /usr/bin/ControlPanel ControlPanel /usr/lib/jvm/java-7-oracle/jre/bin/ControlPanel 100
# update-alternatives --quiet --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/jre/bin/java 100
# update-alternatives --quiet --install /usr/bin/java_vm java_vm /usr/lib/jvm/java-7-oracle/jre/bin/java_vm 100
# update-alternatives --quiet --install /usr/bin/javaws javaws /usr/lib/jvm/java-7-oracle/jre/bin/javaws 100
# update-alternatives --quiet --install /usr/bin/jcontrol jcontrol /usr/lib/jvm/java-7-oracle/jre/bin/jcontrol 100
# update-alternatives --quiet --install /usr/bin/keytool keytool /usr/lib/jvm/java-7-oracle/jre/bin/keytool 100
# update-alternatives --quiet --install /usr/bin/pack200 pack200 /usr/lib/jvm/java-7-oracle/jre/bin/pack200 100
# update-alternatives --quiet --install /usr/bin/policytool policytool /usr/lib/jvm/java-7-oracle/jre/bin/policytool 100
# update-alternatives --quiet --install /usr/bin/rmid rmid /usr/lib/jvm/java-7-oracle/jre/bin/rmid 100
# update-alternatives --quiet --install /usr/bin/rmiregistry rmiregistry /usr/lib/jvm/java-7-oracle/jre/bin/rmiregistry 100
# update-alternatives --quiet --install /usr/bin/unpack200 unpack200 /usr/lib/jvm/java-7-oracle/jre/bin/unpack200 100
# update-alternatives --quiet --install /usr/bin/orbd orbd /usr/lib/jvm/java-7-oracle/jre/bin/orbd 100
# update-alternatives --quiet --install /usr/bin/servertool servertool /usr/lib/jvm/java-7-oracle/jre/bin/servertool 100
# update-alternatives --quiet --install /usr/bin/tnameserv tnameserv /usr/lib/jvm/java-7-oracle/jre/bin/tnameserv 100
# update-alternatives --quiet --install /usr/bin/jexec jexec /usr/lib/jvm/java-7-oracle/jre/lib/jexec 100

Now you can use update-alternatives to select newly installed Java SDK.

# update-alternatives --config java              # Select java-1.7.0-oracle
# update-java-alternatives --set java-1.7.0-oracle
# exit
$

This worked for me, if there is a more elegant way (without using third-party PPAs) I'd be glad to hear about it. I still need to test Firefox if I can run Java in it.

Share:
101

Related videos on Youtube

GoneInsane
Author by

GoneInsane

Updated on September 18, 2022

Comments

  • GoneInsane
    GoneInsane over 1 year

    Whenever the code below is run the right row is made in the right places in the table but then it makes an extra row with the username blank but the password with this md5 d41d8cd98f00b204e9800998ecf8427e which is blank as well.

    The code:

    define("DB_SERVER", "localhost");
    define("DB_USER", "will");
    define("DB_PASS", "blahblah");
    define("DB_NAME", "blah");
    define("TBL_USERS", "users");
    
    function addNewUser($username, $password){
       global $connection;
       $password1 = md5($password);
       $username = mysql_real_escape_string($username);
       $q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password1')";
       return mysql_query($q, $connection);
    }    
    
    $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_NAME, $connection) or die(mysql_error());
    addNewUser($_POST["username"], $_POST["password"]);
    }
    

    How do I stop it from doing this? That code is run in a php script by the way.

    • async8192
      async8192 over 12 years
      I wrote a blog post with a very detailed explanation on how to install Java 7 on Natty, both OpenJDK's and Sun's: http://brunoreis.com/tech/installing-java-ubuntu-natty/ OpenJDK is installed from the PPA provided by Damien Lecan, and Sun's is installed manually. As far as know, until now that is the only guide that is correct both in the steps and the explanation. If you find any problems, post a comment there and I will try to fix it. Good luck.
    • Jose Silva
      Jose Silva over 11 years
      I would also like to point out that using OpenJDK is more than a viable alternative: it is also the reference implementation now.
    • Elzo Valugi
      Elzo Valugi about 11 years
      openJdk is not supported by banks applets or by other software. example: Aptana Studio
    • Philippe Paré
      Philippe Paré over 10 years
      link to "this way" seems to reveal...no description of a way?
    • GKFX
      GKFX about 9 years
      @ElzoValugi I think you might need Iced Tea to use bank applets. It's based on OpenJDK.
    • Admin
      Admin almost 6 years
      How come you never accepted an answer?
    • Admin
      Admin almost 6 years
      Possible duplicate of How do I install Java?
    • lealceldeiro
      lealceldeiro over 5 years
      Alternatively you can use sdkman.io. I posted a full answer here stackoverflow.com/questions/49507160/…
    • Hedley Finger
      Hedley Finger almost 5 years
      THIS PAGE IS WAY OUT OF DATE. Oracle Java 6/7/9 have reached end of life. Java 8 cannot be installed from the webupd8team PPA as the Oracle licence was changed in April 2019. You can now only use the method of downloading the *.tar.gz archive and following the installation method below.
  • Jess
    Jess about 13 years
    If he isn't running a query in the function, would he still need the global connection variable?
  • cwharland
    cwharland over 12 years
    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?
  • Marcin Gil
    Marcin Gil over 12 years
    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).
  • ccd
    ccd about 12 years
    good and relatively painless solution on Precise, just installed jdk1.7.0_03 on it
  • Medet Tleukabiluly
    Medet Tleukabiluly about 12 years
    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?
  • henrique
    henrique about 12 years
    @Shoan, in that case, go to step "if only one alternative is shown then remember the number 0"
  • David Barker
    David Barker about 12 years
    Thank you for this answer, Oracles instructions are rubbish for a linux noob, this made things alot easier!
  • nilsonneto
    nilsonneto about 12 years
    ... just friendly advice - check oracle's redistribution license ... you dont want oracle's lawyers chasing you!
  • Eliah Kagan
    Eliah Kagan almost 12 years
    The instructions you provide to add a plugin for Firefox or Chrome do not affect Chrome at all. Chrome does not use the contents of the .mozilla folder.
  • Scaine
    Scaine almost 12 years
    Good shout! Wasn't working on my 12.04 64-bit until I read your answer. Thanks!
  • weima
    weima almost 12 years
    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
  • umop aplsdn
    umop aplsdn over 11 years
    @SorinSbarnea Some people don't like to use third party repositories because they aren't always updated or online for that matter.
  • mafrax
    mafrax over 11 years
    I've had so many issues with this script -_-
  • mniess
    mniess over 11 years
    @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.
  • mafrax
    mafrax over 11 years
    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.
  • ThiagoPonte
    ThiagoPonte over 11 years
    Many thanks! This is what was missing for my java to work on firefox.
  • JamesTheAwesomeDude
    JamesTheAwesomeDude over 11 years
    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...)
  • thecoshman
    thecoshman over 11 years
    Worth noting for those who have not got any previous version of Java installed, you do not need to preform the command sudo update-alternatives --config java (no need for either occurrence) It will not cause problems if you do though, it will simply give you a warning that there are no or only one alternative and nothing is to be configured
  • Mark Butler
    Mark Butler about 11 years
    On Ubuntu server, I had to use "sudo apt-get install python-software-properties" to get the add-apt-repository command.
  • dstarh
    dstarh about 11 years
    @SorinSbarnea because the repository is generally well out of date
  • Brad
    Brad about 11 years
    While this method works - apt-get won't know that you have java installed. So trying to install tools like ant, maven, etc will still want to install openjdk. Instead something like webupd8.org/2012/01/… worked great for me!
  • djangofan
    djangofan almost 11 years
    I am noticing that the 'update-java-alternatives' commmand from the java-common package doesn't work with Oracle JDK. Anyone know why?
  • gregmac
    gregmac almost 11 years
    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.
  • Mads Skjern
    Mads Skjern over 10 years
    What if update-alternatives --config java gives update-alternatives: error: no alternatives for java?
  • Mads Skjern
    Mads Skjern over 10 years
    I really doubt that the latter instructions help for Chrome, ie does Chrome really read the settings for Firefox??
  • Fatih Karatana
    Fatih Karatana over 10 years
    Thanks for answer. Only this way made my Chrome JRE worked!
  • mamdouh alramadan
    mamdouh alramadan over 10 years
    tried it on ubuntu 13.04 and it works. +1 thanks
  • Rajat Gupta
    Rajat Gupta over 10 years
    does it install just the jre or entire jdk, if entire jdk, then what's way to restrict installation only to jre ?
  • Rajat Gupta
    Rajat Gupta over 10 years
    does this type of installation provide me with automatic updates as they are released & wont adding this ppa add other kinds of softwares unknowingly to my server?
  • paul
    paul over 10 years
    1.7.0_40 works on xubuntu 13.04 too. fast and simple.
  • Rajat Gupta
    Rajat Gupta over 10 years
    can you think of any need for firefox/chrome plugin for JRE on a production web server ? Also, It would be great if you could also add to this answer about how to update these installations later with security patches etc or newer versions(btw OP also asked for that already).
  • DavidGamba
    DavidGamba over 10 years
    How about the man ($JAVA_HOME/man) pages? you need to --slave them as in @BigWhale's answer. Also, are the jars located in lib ($JAVA_HOME/lib) needed? How do you properly install those as well?
  • DavidGamba
    DavidGamba over 10 years
    This is the only solution that shows how to add the man pages!
  • DavidGamba
    DavidGamba over 10 years
    Would this update the alternatives? probably only copy the files.
  • kiri
    kiri over 10 years
    This installs the JDK: "This package provides Oracle Java JDK 7 (which includes Java JDK, JRE and the Java browser plugin). However, you can't only install Oracle JRE - the PPA only provides the full Oracle JDK7 package." ref
  • Jason Southwell
    Jason Southwell over 10 years
    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.
  • Admin
    Admin over 10 years
    2-5 minute? it took 77 minutes
  • pablofiumara
    pablofiumara over 10 years
    Thank you for the tutorial. To make it work in Chromium (32bit), apart from Firefox, run this in terminal: sudo ln -s /usr/lib/jvm/jdk1.7.0/jre/lib/i386/libnpjp2.so /usr/lib/chromium-browser/plugins/ To make it work in Chrome (32bit): sudo mkdir /opt/google/chrome/plugins && cd /opt/google/chrome/plugins && sudo ln -s /usr/lib/jvm/jdk1.7.0/jre/lib/i386/libnpjp2.so /opt/google/chrome/plugins/ In some Ubuntu based distros, if you want to make Flash work in Firefox, just add that file to /usr/lib/firefox-addons/plugins
  • LaundroMat
    LaundroMat over 10 years
    +1 for (a.o.) " Oracle's links are not wget friendly" - I had a hell of a time trying to get these files (headless server, had to install GUI + browser to get at them).
  • ccd
    ccd almost 10 years
    @sorin some of us are working with multiple java versions, and the first answer kinda reminds us of verifying which version is currently active
  • JohnMerlino
    JohnMerlino almost 10 years
    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.
  • Mads Skjern
    Mads Skjern over 9 years
    Also, I believe a restart of Firefox is necessary, so maybe that should be added to the process.
  • landroni
    landroni over 9 years
    -1. This answer is completely out of context..
  • landroni
    landroni over 9 years
    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.
  • Tim
    Tim over 9 years
    How can I install manpage?
  • Anwar
    Anwar over 9 years
    I love manual works, because I want to know what are going on
  • Anwar
    Anwar over 9 years
    Add the information that, if we don't want to remove any existing jdk, then we should skip the first dialog which delete existing jdk
  • Anwar
    Anwar over 9 years
    @DavidG Alien Andrei's solution also works flawlessly
  • Quazi Irfan
    Quazi Irfan over 9 years
    I couldn't find /.mozilla instead I used /usr/lib/firefox-addons/plugins folder and it worked without restarting firefox. Source
  • Anwar
    Anwar over 9 years
    It's not the answer of the question. Instead, it should be a comment
  • Anwar
    Anwar over 9 years
    Also this solution is for 64bit Ubuntu. for 32bit, we should have i386 instead of amd64
  • GKFX
    GKFX about 9 years
  • rubo77
    rubo77 almost 9 years
    Is this still needed? What is the benefint against installing java over the PPA from ppa:webupd8team/java? (first answer here)
  • Donato
    Donato almost 9 years
    Isn't /usr/lib meant for package distributions? If you are using source, I assume it would be put in /usr/local instead. Probably /usr/local/java8 would be a good location.
  • Shikloshi
    Shikloshi over 8 years
    How can I install both jdk7 and jdk8 in the manual way?
  • vivi
    vivi over 8 years
    Use proper filename when using terminal for example use ' sudo mv ./jdk1.8.0_66 /usr/lib/jvm/ ' when using java jdk version 8 update 66
  • Hi-Angel
    Hi-Angel over 8 years
    @Shoan I had the same error, however for Java to work in a browser was enough to skip the «alternatives» step, and make the symlink.
  • JonasCz
    JonasCz almost 8 years
    Note that that Webupd8 PPA also contains a script which will set Oracle Java as default (useful if you have multiple JRE's / JDKs installed), install it with sudo apt-get install oracle-java8-set-default. Also note that this answer is a bit out of date, you should probably be installing oracle-java8-installer these days.
  • Onix
    Onix over 7 years
    I get this error update-alternatives: error: alternative path is not absolute as it should be: usr/lib/jvm/jdk1.8.0_102/bin/java after running sudo update-alternatives --install "/usr/bin/java" "java" "usr/lib/jvm/jdk1.8.0_102/bin/java" 1 what m i doing wrong thanks.
  • Anwar
    Anwar about 7 years
    /usr/bin/java is not a folder, that's why the error
  • Ahmed Mostafa
    Ahmed Mostafa over 6 years
    I tried the easy way and I got this error E: Package 'oracle-java8-installer' has no installation candidate
  • Nathan F.
    Nathan F. almost 5 years
    This no longer works, the webupd8team/java PPA has been discontinued.
  • Amor.o
    Amor.o almost 4 years
    Thank you, this should be a tutorial not an answer