Q. How do I install Sun Java Development Kit (JDK) and Java Runtime Environment (JRE) under Ubuntu Linux? It appears that there are multiple JRE installed by default under Ubuntu. How do I select and use Sun JRE only? Can you explain steps required to set the environment to run java programs or apps?
A. Ubuntu Linux 7.10 has following packages from Sun:
=> sun-java6-bin : Sun Java Runtime Environment (JRE) 6
=> sun-java6-demo : Sun Java Development Kit (JDK) 6 demos
=> sun-java6-jdk : Sun Java Development Kit (JDK) 6
=> sun-java6-jre : Sun Java Runtime Environment (JRE) 6
Install Sun Java 6
To install proprietary Java, you must have the Multiverse repository enabled. Click on System > Administration > Software Source > Select Multisource > Close

Open a shell prompt (terminal) and type the following to install JDK and JRE:
$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
Setup the default Java version
Ubuntu Linux comes with update-java-alternatives utility to updates all alternatives belonging to one runtime or development kit for the Java language. To select, Sun's JVM as provided in Ubuntu 7.10, enter:
$ sudo update-java-alternatives -s java-6-sun
You also need to edit a file called /etc/jvm. This file defines the default system JVM search order. Each JVM should list their JAVA_HOME compatible directory in this file. The default system JVM is the first one available from top to bottom. Open /etc/jvm
$ sudo vi /etc/jvm
Make sure /usr/lib/jvm/java-6-sun is added to the top of JVM list
/usr/lib/jvm/java-6-sun
At the end your file should read as follows:
/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-gcj
/usr/lib/jvm/ia32-java-1.5.0-sun
/usr/lib/jvm/java-1.5.0-sun
/usr
Save and close the file.
Setup the environment variable
You also need to setup JAVA_HOME and PATH variable. Open your $HOME/.bash_profile or /etc/profile (system wide) configuration. Open your .bash_profile file:
$ vi $HOME/.bash_profile
Append following line:
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin
Save and close the file.
Test your new JDK
Type the following command to display version:
$ java -version
Output:
java version "1.6.0_03" Java(TM) SE Runtime Environment (build 1.6.0_03-b05) Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)
Try HelloWorld.java - first java program
$ vi HelloWorld.java
Append code:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Save and close the file. Compile HelloWorld.java and execute program by typing following two instructions:
$ javac HelloWorld.java
$ java HelloWorld
Output:
Hello, World!
Further readings:
- Official Sun Java tutorial - The Java Tutorials are practical guides for programmers who want to use the Java programming language to create applications. They include hundreds of complete, working examples, and dozens of lessons. Groups of related lessons are organized into "trails".
- man pages java, javac
- Email FAQ to a friend
- Printable version
- Rss Feed
- Last Updated: 9-17-08

{ 36 comments… read them below or add one }
When I execute:
$ javac HelloWorld.java
Show me this error:
El programa «javac» puede encontrarse en los siguientes paquetes:
* jikes-sun
* jikes-sablevm
* gcj-4.2
* kaffe
* ecj
* java-gcj-compat-dev
* j2sdk1.4
* jikes-classpath
* jikes-gij
* gcj-4.1
* sun-java5-jdk
* jikes-kaffe
* sun-java6-jdk
Pruebe: sudo apt-get install
bash: javac: orden no encontrada
Thanks, any help would be greatly appreciated.
Miguel:
I had the same problem. I solved it by running:
sudo update-java-alternatives -s java-6-sun
You will also need to install sun-java6-jdk if you have not done that already
WinRAR en Java Sun JDk 6 download
thanks. works
$ sudo apt-get install sun-java6-bin sun-java6-jre
should be
$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
to solve the compile problem
thx dru (:
Thanks for the heads up. The faq has been updated.
Thanks yeah!!!! I could install java… I m not a newbie but was still facing lots of problem… and all was resolved following these simple steps religiously….
This won’t work in 64-bit.
I follow the instructions. but if I type
$ java -version
java version “1.5.0_14″
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_14-b03)
Java HotSpot(TM) Client VM (build 1.5.0_14-b03, mixed mode, sharing)
jdk1.5.0_14 installed at /usr/java/jdk1.5.0_14/
should I remove jdk1.5.0_14 first?
thx
Hi,
When I build the opennms-1.5.93 in Ubutu, when I entered the ./build.sh compile. I will have the following error:
java.lang.OutOfMemoryError: PermGen space
Anyone could help me ?
Thanks in advance.
John
Thanks for this. I’ve been setting up several Ubuntu laptops for students in my AP Computer Science class and this was very helpful.
Thanks, nice tutorial.
Apparently I was able to install SE Runtime Environment build 1.6.0_03-b05 successfully since it shows up on my terminal window when i call it up. I still am not able to get my active window in my stock account which went away when I installed Firefox 3.01. When I was working in FF 3, I had to install the plug in to make it work. What else do I need?
Ubuntu 7.10 Firefox 3.01
Hi, i need your help.
when i going to install some third party program,
it pop up this message:
PLUGIN_JAVA_HOME
PLUGIN_JAVA_HOME
please set PLUGIN_JAVA_HOME
any one can help me on this?
Above under Setup Default Java Version, at the end it says to save and close the file. How do you save? I can’t find a command for ’save’.
mike:
You can also use the nano editor, which may be easier for you.
But, if you’re using vi/vim:
:wq
will write the file and exit it.
I am a beginner,I am having a problem in doing the following steps :
You also need to edit a file called /etc/jvm. This file defines the default system JVM search order. Each JVM should list their JAVA_HOME compatible directory in this file. The default system JVM is the first one available from top to bottom. Open /etc/jvm
$ sudo vi /etc/jvm
Make sure /usr/lib/jvm/java-6-sun is added to the top of JVM list
/usr/lib/jvm/java-6-sun
At the end your file should read as follows:
/usr/lib/jvm/java-6-sun
/usr/lib/jvm/java-gcj
/usr/lib/jvm/ia32-java-1.5.0-sun
/usr/lib/jvm/java-1.5.0-sun
/usr
I had done all the steps in which i am facing problem , Hello world program works nicely but when i run the program of classes I am getting the error that is :
Caused by: java.lang.ClassNotFoundException: Classes
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
PLese help me
Am now internet banking using firefox and my bank’s java dependent website, thanks.
Hi@
very helpful, all problems around “jaolt” are solved with this entry
thx
J
Thanks for the great writeup! This is exactly what I was looking for and I was up and running in no time.
Hi, for a course I have to use java again and I was looking for a quick installation guide and yours did the trick on Ubuntu 8.10! Thanks
hey!!!
i followed the instruction but i m not able install jdk in linux..
plse help me….
i m
:~$ sudo apt-get install sun-java5-jdk
[sudo] password for sebastian:
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
Since you only requested a single operation it is extremely likely that
the package is simply not installable and a bug report against
that package should be filed.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
sun-java5-jdk: Depends: sun-java5-demo (= 1.5.0-16-2ubuntu2) but it is not going to be installed
Depends: sun-java5-jre (= 1.5.0-16-2ubuntu2) but it is not going to be installed
E: Broken packages
sebastian@sebastian-laptop:~$
any ideas why this can’t be done?
Thanks pal!
Thanks.
This is very helpful.
i am try but i am not able to install jdk so please help me
I was having trouble with the /etc/jvm part, so I just tried running one of my own java programs and they seemed to work fine.
jee@jee-desktop:~$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
E: dpkg was interrupted, you must manually run ‘dpkg –configure -a’ to correct the problem.
anyone can help me.After i installed java in ubuntu8.04,i got this error.
thanks a lot these informations are very helpfull=)I solved completly my problems about that…
Thanks a lot for the post! Anything that’s not config/make/make install is a bit confusing to me — Ubuntu’s like Windows with less-slick graphics :) Anyway, no religious wars — your post was *really* helpful.
Thanks for your nice nice Article.
Plase help me I’m having same as VANI.Now I can not able to install any soft in my Ubuntu.Please PLEASE help me
thanks… helpfull!!!!
Why is the Ubuntu multiverse repository for Sun-JDK 1.6 only on 1.6.0_07 while Sun says it’s up to 1.6.0_14?