Check if you have Java installed on your system. To do this, you will have to run the Java version command from terminal.
Open up a terminal and enter the following command:
Type/Copy/Paste: java -version
If you have OpenJDK installed on your system it may look like this:
java version "1.6.0_21"
OpenJDK Runtime Environment (IcedTea6 1.10pre) (6b21~pre1-0lucid1)
OpenJDK 64-Bit Server VM (build 19.0-b09, mixed mode)
If you have OpenJDK installed on your system, you have the wrong Java installed for this exercise. We want it to display:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
Completely remove the OpenJDK/JRE from the system if necessary. This will prevent system conflicts and confusion between different vendor versions of Java. For example, if you have the OpenJDK/JRE installed on your system, you can remove it by typing the following at the command line:
Type/Copy/Paste: sudo apt-get autoremove openjdk-6-jre
Type/Copy/Paste: sudo apt-get autoremove openjdk-6-jdk
Create a directory to put your Oracle Java JDK and JRE binaries in, open up a terminal and create the directory /usr/local/java
Type/Copy/Paste: sudo mkdir -p /usr/local/java
Download the Oracle Java JDK/JRE for Linux. Make sure you select the compressed binaries for your system (which end in tar.gz).
Move the Oracle Java binaries into the /usr/local/java directory. In most cases, the Oracle Java binaries are downloaded to: /home/your_user_directory/Downloads.
Type/Copy/Paste: sudo -s
Type/Copy/Paste: cd /home/"your_user_directory"/Downloads
Type/Copy/Paste: cp -r jdk-7u4-linux-x64.tar.gz /usr/local/java
Type/Copy/Paste: cp -r jre-7u4-linux-x64.tar.gz /usr/local/java
Type/Copy/Paste: cd /usr/local/java
Run the following commands on the downloaded Oracle Java tar.gz files. Make sure to do this as root in order to make them executable for all users on your system.
Type/Copy/Paste: sudo chmod a+x jdk-7u4-linux-x64.tar.gz
Type/Copy/Paste: sudo chmod a+x jre-7u4-linux-x64.tar.gz
Unpack the compressed java binaries, in the directory /usr/local/java
Type/Copy/Paste: tar xvzf jdk-7u4-linux-x64.tar.gz
Type/Copy/Paste: tar xvzf jre-7u4-linux-x64.tar.gz
Double-check your directories. At this point, you should have two uncompressed binary directories in /usr/local/java for the Java JDK/JRE listed as:
jdk1.7.0_04
jre1.7.0_04
Edit the system PATH file /etc/profile and add the following system variables to your system path. Use nano, gedit or any other text editor, as root, open up /etc/profile, and add either:
Type/Copy/Paste: sudo nano /etc/profile or
Type/Copy/Paste: sudo gedit /etc/profile
Scroll down to the end of the file using your arrow keys and add the following lines below to the end of your /etc/profile file:
Type/Copy/Paste:
JAVA_HOME=/usr/local/java/jdk1.7.0_04
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
JAVA_HOME=/usr/local/java/jre1.7.0_04
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH
Save the /etc/profile file and exit.
Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located. This will tell the system that the new Oracle Java version is available for use.
Type/Copy/Paste: sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_04/bin/java" 1
Type/Copy/Paste: sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_04/bin/javac" 1
Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java.
Type/Copy/Paste: sudo update-alternatives --set java /usr/local/java/jre1.7.0_04/bin/java
Type/Copy/Paste: sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_04/bin/javac
Reload your system wide PATH /etc/profile by typing the following command:
Type/Copy/Paste: . /etc/profile
Note your system-wide PATH /etc/profile file will reload after reboot of your Ubuntu Linux system
Test to see if Oracle Java was installed correctly on your system. Run the following commands and note the version of Java:
Type/Copy/Paste: java -version
This command displays the version of java running on your system
You should receive a message which displays:
java version "1.7.0_04"
Java(TM) SE Runtime Environment (build 1.7.0_04-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.0-b21, mixed mode)
Type/Copy/Paste: javac -version
This command lets you know that you are now able to compile Java programs from the terminal.
You should receive a message which displays:
javac 1.7.0_04
Reboot your Ubuntu Linux system. Afterwards, your system will be fully configured for running and developing Java programs.