Abstract
This blog entry will guide you through the step-by-step installation of Java on Ubuntu. I selected Oracle Java 7 update 9 and Ubuntu Linux 12.10 bit for this post.
Introduction
Installing Java on Linux follows the download-extract-configure pattern. We will begin by downloading Oracle Java from Oracle’s website, extracting the download in the appropriate folder, and finally informing Ubuntu about the newly installed version of Java.
Step 1: Verify that you do not already have the correct version of Java installed.
Open your console window and enter the following command:
java –version
If you get the following result, you already have Java 7 update 9 installed and can ignore the rest of the steps:
java version 1.7.0_09 Java(TM) SE Runtime Environment (build 1.9.0_09-b05) Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode)
Step 2: Download Oracle Java.
New release of Java are featured on the main Java download page.
If Java 7 update 9 is no longer featured, you can find the download by following the Previous Releases link found on the main download page.
Open the Java download page in your browser and download both jdk-7u9-linux-i586.bin and jre-7u9-linux-i586.bin
Make a note of the folder to which you downloaded the files. For further reference in this blog, I will call this folder the “downloads folder”.
Step 3: Create the installation folder.
The usr/lib/jvm is the default installation location of the Java JDK and the Java JRE. Enter the following command in your console to create this folder, if it does not already exist:
sudo mkdir -p /usr/lib/jvm
The –p option ensures that all folders in the mkdir path are created.
Step 4: Navigate to the “downloads folder”.
If you downloaded the files to your Home folder, you can use the following command:
cd ~/
or substitute "~/" with the path to the “downloads folder”.
Step 5: Move the downloaded files to the installation folder.
sudo mv jdk-7u9-linux-i586.tar.gz /usr/lib/jvm sudo mv jre-7u9-linux-i586.tar.gz /usr/lib/jvm
Step 6: Navigate to the “installation folder”.
cd /usr/lib/jvm
Step 7: Unpack the tarball archives.
sudo tar zxvf jdk-7u9-linux-i586.tar.gz sudo tar zxvf jre-7u9-linux-i586.tar.gz
If you want to conserve space to may delete the tarball archives.
sudo rm jdk-7u9-linux-i586.tar.gz sudo rm jre-7u9-linux-i586.tar.gz
Step 8: Display the contents of the installation folder.
ls -l
Response:
jdk1.7.0_09 jre1.7.0_09
Make a note of the newly created folder names.
Step 9: Inform Ubuntu where your Java installation is located.
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.7.0_09/bin/javac" 1 sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jre1.7.0_09/bin/java" 1
Step 10: Inform Ubuntu that this is your default Java installation.
sudo update-alternatives --set "javac" "/usr/lib/jvm/jdk1.7.0_09/bin/javac" sudo update-alternatives --set "java" "/usr/lib/jvm/jre1.7.0_09/bin/java"
Step 11: Update your system-wide PATH.
Edit your /etc/profile file using:
sudo nano /etc/profile
Add the following entries to the bottom of your /etc/profile file:
JAVA_HOME=/usr/lib/jvm/jdk1.7.0_09 PATH=$PATH:$JAVA_HOME/bin export JAVA_HOME export PATH
Save your /etc/profile file using CTRL + X.
Step 12: Reload your system-wide PATH.
. /etc/profile
Step 13: Test your new installation.
java –version
Response:
java version 1.7.0_09 Java(TM) SE Runtime Environment (build 1.9.0_09-b05) Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode)
javac –version
Response:
javac 1.7.0_09
That’s what I get on step 11:
bodu4ka@bodu4ka-1000HE:/usr/lib/jvm$ echo “JAVA_HOME=/usr/lib/jvm/jdk1.7.0_09″ >> /etc/profile
bash: /etc/profile: Permission denied
bodu4ka@bodu4ka-1000HE:/usr/lib/jvm$ echo “PATH=$PATH:$JAVA_HOME/bin” >> /etc/profile
bash: /etc/profile: Permission denied
bodu4ka@bodu4ka-1000HE:/usr/lib/jvm$ echo “export JAVA_HOME” >> /etc/profile
bash: /etc/profile: Permission denied
bodu4ka@bodu4ka-1000HE:/usr/lib/jvm$ echo “export PATH” >> /etc/profile
bash: /etc/profile: Permission denied
Mode:
bodu4ka@bodu4ka-1000HE:/usr/lib/jvm$ ls -l /etc/profile
-rw-r–r– 1 root root 665 Nov 23 21:28 /etc/profile
Any idea?
Thank you!
I have updated step 11. You need to run the commands as the super user.
Thanks so much for the great article. All worked like a charm. Appreciate the help.
i work
thanks
In step 11, you have:
sudo echo “PATH=$PATH:$JAVA_HOME/bin” >> /etc/profile
What this did was actually parse the $PATH and $JAVA_HOME environment variables before adding the line to /etc/profile. At that point, $JAVA_HOME isn’t defined either so what it does is add :/bin to /etc/profile.
I had to switch user to root:
sudo -s -H
And edit /etc/profile manually in order to add the line
PATH=$PATH:$JAVA_HOME/bin
as you intended it to be added in your step 11.
The JAVA_HOME variable is defined first. Did you assign this before your assigned the PATH variable? Here are the steps again:
sudo echo "JAVA_HOME=/usr/lib/jvm/jdk1.7.0_09" >> /etc/profile
sudo echo "PATH=$PATH:$JAVA_HOME/bin" >> /etc/profile
sudo echo "export JAVA_HOME" >> /etc/profile
sudo echo "export PATH" >> /etc/profile
Note that JAVA_HOME is not preceded with the $ symbol, it therefore will not be parsed.
After step 11 i’m stil getting ; bash: /etc/profile: line 37: syntax error: unexpected end of file
Even when the following is pasted;
sudo echo “JAVA_HOME=/usr/lib/jvm/jdk1.7.0_09″ >> /etc/profile
sudo echo “PATH=$PATH:$JAVA_HOME/bin” >> /etc/profile
sudo echo “export JAVA_HOME” >> /etc/profile
sudo echo “export PATH” >> /etc/profile
You seem to have followed steps in the comments and not the steps in the actual post. The command
sudo echodo not appear in Step 11. Please follow the Step 11 instructions in the blog post. Let me know if your issue persists.Seems that at step 13 i had to put sudo in front. Got it installed (ThankGod)