Java command not found even after "sudo apt-get install openjdk-6-jdk"

I followed the instruction to set up Java on Ubuntu 11.10 from How do I install Java?.

I ran this command to install:

sudo apt-get install openjdk-6-jdk

After it succeeded and I typed java, I got this error:

The program 'java' can be found in the following packages:
 * gcj-4.4-jre-headless
 * gcj-4.6-jre-headless
 * openjdk-6-jre-headless
 * gcj-4.5-jre-headless
 * openjdk-7-jre-headless

Solution 1:

Did you try this command?

sudo update-alternatives --config java

This will list all the Java versions installed and configured.

If you see one or more installs, it will give you a list each element starting with a number. Just choose the number that corresponds to the version you want to use by default.

If you don't see any installed then something is not right. In that case take a look at this question.

While that question is for Oracle Java 7, it lists methods common to both Java 6 and Java 7, for installing Java and getting Ubuntu to see and link to the new Java installation. If you want version 6, just download the version 6 JDK and follow the instructions provided in that question, changing the filename accordingly.

Solution 2:

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 historical reasons.

Install Oracle Java using the following commands

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

Solution 3:

Solved this by manually setting java path with /etc/profile.

Add following lines to there.

JAVA_HOME=/usr/lib/jvm/java-6-openjdk
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH