Ubuntu Linux Install Sun Java Development Kit ( JDK ) and Java Runtime Environment ( JRE )

by Vivek Gite · 61 comments

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
Adding the Universe and Multiverse Repositories

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

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 61 comments… read them below or add one }

1 Miguel 03.12.08 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.

2 Tor 03.15.08 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

3 ahmed 05.11.08 at 8:04 pm

WinRAR en Java Sun JDk 6 download

4 jose 06.08.08 at 6:46 am

thanks. works

5 dru 06.19.08 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

6 Pewo 07.01.08 at 7:22 pm

thx dru (:

7 vivek 07.01.08 at 8:55 pm

Thanks for the heads up. The faq has been updated.

8 pall 07.03.08 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….

9 Thelasko 08.12.08 at 4:18 pm

This won’t work in 64-bit.

10 yadhi 08.20.08 at 10:55 am

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

11 John 08.22.08 at 10:52 am

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

12 Richard 09.06.08 at 11:03 pm

Thanks for this. I’ve been setting up several Ubuntu laptops for students in my AP Computer Science class and this was very helpful.

13 Deepak 09.19.08 at 11:36 am

Thanks, nice tutorial.

14 Jud 09.23.08 at 3:51 am

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

15 chrond 10.18.08 at 10:44 am

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?

16 mike 10.20.08 at 7:09 pm

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’.

17 mfragin 10.21.08 at 1:02 am

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.

18 John glenn 11.24.08 at 4:39 am

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

19 John glenn 11.25.08 at 6:51 am

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

20 Cyclotron 12.18.08 at 7:25 am

Am now internet banking using firefox and my bank’s java dependent website, thanks.

21 JBusse 12.27.08 at 8:34 pm

Hi@
very helpful, all problems around “jaolt” are solved with this entry
thx
J

22 JP Grace 01.22.09 at 5:25 am

Thanks for the great writeup! This is exactly what I was looking for and I was up and running in no time.

23 Niels 02.04.09 at 10:03 am

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

24 nikhil ranjan 02.08.09 at 12:56 pm

hey!!!
i followed the instruction but i m not able install jdk in linux..
plse help me….
i m

25 Sebastian 02.14.09 at 7:43 am

:~$ 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?

26 ShireFolk 03.23.09 at 11:56 am

Thanks pal!

27 Ejaz 03.24.09 at 7:38 am

Thanks.
This is very helpful.

28 sunil 04.14.09 at 11:15 am

i am try but i am not able to install jdk so please help me

29 hero12 04.17.09 at 5:02 pm

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.

30 vani 04.23.09 at 10:35 am

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.

31 Moonlight 05.01.09 at 10:31 am

thanks a lot these informations are very helpfull=)I solved completly my problems about that…

32 eprue 05.21.09 at 6:55 am

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.

33 Romel 05.30.09 at 8:07 am

Thanks for your nice nice Article.

34 Ove 06.10.09 at 3:13 am

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

35 Guntur 07.02.09 at 5:19 am

thanks… helpfull!!!!

36 Steve Cohen 07.02.09 at 9:10 pm

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?

37 tpcool 07.16.09 at 10:14 pm

Thanks very helpful

38 chaudhari mohisn 07.25.09 at 2:45 pm

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?

39 chaudhari mohisn 07.25.09 at 2:47 pm

thats vary nice

40 rubson 08.03.09 at 4:29 am

step by step
excelent.
java sun it’s work !

41 Tynen 08.05.09 at 10:51 pm

Thank you. :)

42 sabu 08.08.09 at 3:58 am

i see instal the java instructin .that methord folow.cannot instal java in ubuntu 8.04 lts.please help me

43 Neal 08.16.09 at 8:39 pm

Excellent – concise, accurate and easy. Thank you.

44 sabu 08.24.09 at 12:20 pm

thank u for previous information.
i install some software in way to terminal.that time i see dpkg error i ubuntu 8.04.I can’t install softwares.
please help me.

45 alves 09.07.09 at 1:58 pm

I am facing same problem as vani face.
:(
help me out

46 ayub 09.14.09 at 10:30 am

when i input the root password on the terminal i get “sorry try again” HELP

47 Mike 09.29.09 at 8:38 pm

I have done the steps and it all works but I cant get Scanner working when I write code anyone got any idea why not.

48 Aris Boch 11.01.09 at 10:15 am

When i want to run a java prog any more sophisticated than hello world or similar, the jvm gives me the following:
baruch@baruch-laptop:~/jdownloader$ java -jar jdupdate.jar
Exception in thread “main” java.lang.NoClassDefFoundError: jd.update.Main
at java.lang.Class.initializeClass(libgcj.so.90)
Caused by: java.lang.ClassNotFoundException: com.sun.java.swing.plaf.windows.WindowsLookAndFeel not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:jdupdate.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.90)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at java.lang.ClassLoader.loadClass(libgcj.so.90)
at java.lang.Class.forName(libgcj.so.90)
at java.lang.Class.initializeClass(libgcj.so.90)
What is wrong with my current java installation (i have installed an parallel one, it worked like a charm). Does anybody know how to solve this problem? And how to set the parallelly installed jvm to be the standard one? TIA.

49 Vivek Gite 11.01.09 at 10:37 am

You are using GNU java. Install Sun JAVA and switch to it.

50 Aris Boch 11.01.09 at 11:00 am

I have installed Sun java. How to “switch”? TIA.

51 Vivek Gite 11.01.09 at 12:06 pm

Run update-java-alternatives -l to all installed java versions:
update-java-alternatives -l
Set it to Sun java
update-java-alternatives -s java-6-sun
Verify it:
java -version

52 Aris Boch 11.01.09 at 12:15 pm

Works! Java VM ist alive and kicking again:) Thx a lot!!!!

53 anon 11.22.09 at 6:45 pm

MicroSoft has nothing to worry about.
I thought I would try Ubuntu on an old computer.
I got Ubuntu 9.1 installed and running.
Then I just spent 5 frustrating hours TRYING to install Java jdk on Ubuntu 9.1. WTF!!!
Monday is trash day. That old computer with Ubuntu 9.1 Will be in the pile.

54 Henry James 11.24.09 at 3:57 am

Remember to set “JAVA_JDK” for intelliIDE to run. Also make sure it’s not using openjava jdk which may have installed if you installed icetea jre in firefox, then you’re JRE and JDK may have switched. Remember to reset your JAVA_HOME env (which java install never does for you) if you updated and they’ve decided to break your symlink. Updated Ubuntu? No problem someone will have decided that .bash_profile is now to be called .bachrc, why you ask? God knows – but I’m sure they will change it back again to piss you off on the next update. Java would be great if it wasn’t for all the moronic bullshit you have to configure and their insane versioning… oh yeah it’s JEE now with Java6 that I work with not 1.6!!. Couple this with Linux vendors stupid changing of config files with every release and you’ve got one giant mess.

55 Asterios 11.30.09 at 11:33 pm

Hello. Sorry for my english but i am not good in this language. I have a problem. I compile a java file which has this class: class Pan extends java.applet.Applet implements Runnable { …

but i have many warnings like this : warning: [deprecation] size() in java.awt.Component has been deprecated —- x1 = size().width
I make compile : javac name.java -Xlint

Is anybody who can help me please????

Thank you.

56 Nick 12.01.09 at 12:44 am

Hi, most everything went well except when I did:

$ sudo update-java-alternatives -s java-6-sun
update-alternatives: error: no alternatives for mozilla-javaplugin.so.
update-alternatives: error: no alternatives for pluginappletviewer.
update-alternatives: error: no alternatives for xulrunner-1.9-javaplugin.so.
update-alternatives: error: no alternatives for xulrunner-1.9-javaplugin.so.

Everything in the write up works though, will this be a problem with other things you think? I am not sure what this line is supposed to accomplish. Can you give me some insight on that? Thanks, much!

57 sarah 12.02.09 at 10:40 am

install java hotspot

58 lee 12.06.09 at 9:19 pm

hello,everyone
i got a problem when i install sun java.
here is errors.
how can i fix it?
apt-get install sun-java6-jre
E: dpkg was interrupted, you must manually run ‘dpkg –configure -a’ to correct the problem.
pls,help

59 prince 12.16.09 at 1:41 am

prince@THOTTATHIL:~$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk
[sudo] password for prince:
E: dpkg was interrupted, you must manually run ’sudo dpkg –configure -a’ to correct the problem.
how to solve this ?

60 prince 12.16.09 at 2:21 am

i solved the above but new problem is i dont know how to do sudo vi /etc/jvm
wen i entered this command to terminal and the following /usr/lib/jvm/java-6-sun
got error E486: pattern not found: usr
pls help me

61 jhang 02.05.10 at 7:20 am

when i’m trying to install java in my ubuntu these message appeared
jhang@ubuntu:~$ sudo apt-get install sun-java6-jdk
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Couldn’t find package sun-java6-jdk

i could not compile my java source code!! HELP PLEASE!!!

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All