How to install openjdk-7-jdk on Ubuntu 12.04 without dragging in unrelated GUI and sound packages?

Solution 1:

UPDATED

I personally try to avoid Java because in my opinion it is very clumsy. These instructions are pieced together from various sources, I had to install Java recently and this should work for you.

If your licensing requirements permit, install Oracle Java. Download java from here, you need to chose which one you need based on what you have installed.

JDK x64

wget --no-check-certificate --no-cookies - --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-x64.tar.gz

JDK x32

wget --no-check-certificate --no-cookies - --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-i586.tar.gz

JRE x64

wget --no-check-certificate --no-cookies - --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jre-7u51-linux-x64.tar.gz

JRE x32

wget --no-check-certificate --no-cookies - --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jre-7u51-linux-i586.tar.gz

Note that you need a workaround for wget and oracle to play nice together.

Create a directory where your Java will live:

sudo mkdir -p /usr/lib/jvm

Move the downloaded Java:

sudo mv jdk-7u51-linux-x64.tar.gz /usr/lib/jvm

Extract Java:

sudo tar zxvf jdk-7u51-linux-x64.tar.gz

Tell Ubuntu that Java exists:

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_51/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_51/bin/java 1
sudo update-alternatives --set javac /usr/lib/jvm/jdk1.7.0_51/bin/javac
sudo update-alternatives --set java /usr/lib/jvm/jdk1.7.0_51/bin/java

Add java path to your system profile, so that the machine knows where the Java binaries are:

sudo nano /etc/profile

At the end of the file add this:

JAVA_HOME=/usr/lib/jvm/jdk1.7.0_51
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Save and reboot.

Test your Java installation:

java -version

Solution 2:

Very old stuff, but if some one is lookig for this yet, just install openjdk-7-jre-headless or openjdk-6-jre-headless