Ubuntu Linux Install Sun Java Development Kit ( JDK ) and Java Runtime Environment ( JRE )
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
Subscribe to our free e-mail newsletter or RSS feed to get all updates.
You can Email this page to a friend.
Related Other Helpful FAQs:
- Ubuntu Linux play encrypted DVDs
- Install Multimedia Codecs For Ubuntu Linux
- Ubuntu Linux: How to Install Flash Player for firefox
- Howto: Ubuntu Linux install Mplayer software
- Ubuntu Linux: How do I install .deb packages?
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: /etc/jvm, export JAVA_HOME=/usr/lib/jvm/java-6-sun, export PATH=$PATH:$JAVA_HOME/bin, install linux jdk, java for ubuntu linux, java linux ubuntu, jre linux ubuntu, linux install jdk, ubuntu install java, Ubuntu Install Java Development Kit, Ubuntu Install Java Runtime Environment, ubuntu installing jdk, Ubuntu Linux Install JDK, Ubuntu Linux Install JRE, ubuntu linux jdk



March 12th, 2008 at 5:13 am
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.
March 15th, 2008 at 2:49 pm
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
May 11th, 2008 at 8:04 pm
WinRAR en Java Sun JDk 6 download
June 8th, 2008 (4 weeks ago) at 6:46 am
thanks. works
June 19th, 2008 (3 weeks ago) at 2:12 am
$ 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
July 1st, 2008 (5 days ago) at 7:22 pm
thx dru (:
July 1st, 2008 (5 days ago) at 8:55 pm
Thanks for the heads up. The faq has been updated.
July 3rd, 2008 (3 days ago) at 8:37 am
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….