Friday, October 5, 2007

HOWTO: install/upgrade SUN Java under linux

Overview
For those of us using a flavor of linux that does not come with SUN Java (which is currently most of us), you may have run into a bit of a problem. Linux distros typically bundle the the GCC GCJ compiler. Sure, it is probably nice and all but I personally prefer SUN Java. It is what is installed on most non-linux based computers out there. It is the standard.

I, like others out there, have probably tried to remove GCJ from their systems. Well, it is not easy since everything Java that comes with the distro is bound to it. Arg!

The next option is to see if you can get SUN Java and GCJ to co-exist, so you install SUN Java anyway, and run
java -version. You still see GCJ's version. Arg! Why? because the GCJ's java is in the path.

You could setup all the path and JAVA_HOME/JAVA_PATH environment variable but, for me, that is just not enough. I want my environment to just use SUN Java. Outside of simply having 1 flavor of Java installed, this is the way to go. Make SUN Java the default Java.


Purpose
The following will describe the rather simple process for getting SUN Java to be the default Java. This is being done under Fedora Core 6 and 7 (RPM based), however, the technique should work for other distros. I am using SUN Java JDK 6 and am upgrading from 6u2 to 6u3. The process of upgrade or install is the same except for removing the old version :-) The most important aspect is the steps to follow.


Required Downloads
The example will be done installing SUN Java JDK 6 Update 3 (6u3) to replace SUN Java JDK 6 Update 2 (6u2).
  • download SUN Java
    • Site: http://java.sun.com/
    • I used the RPM based install package called jdk-6u3-linux-i586-rpm.bin
  • download java-1.6.0-sun-compat-1.6.0.xx-1jpp.i586.rpm where xx is your update version (01, 02, 03,..)
    • Site: http://www.jpackage.org/browser/
    • In this case, we are trying to download a sun-compat for JDK 1.6.0.03 or 6u3. That is what I hope to find...
    • If a version for your JDK is not available, go on to step #3. This is my case for this example.
  • download the source file java-1.6.0-sun-compat-1.6.0.xx-1jpp.src.rpm where xx is the latest available version.
    • Site: http://www.jpackage.org/browser/
    • At the time of the writing, 1.6.0.02 was the only sun-compat available which is a version behind 6u3. This is ok.

Install/Upgrade
  • Install SUN JAVA
    • command: sh jdk-6u3-linux-i586-rpm.bin
    • agree to the terms by typing "yes"
    • You will see it install a bunch of RPMs into /usr/java/ and /opt/sun/
  • If your java 1.6.0-sun-compat RPM version matches your JDK...
    • command: rpm -Uvh java-1.6.0-sun-compat-1.6.0.03-1jpp.i586.rpm
    • you are done
  • If it does not match then install...
    • command: rpm -ivh java-1.6.0-sun-compat-1.6.0.02-1jpp.src.rpm
    • as above, version 02 was the only version availble
    • you are not done yet.
If the 2nd bullet passed and your JDK version (6u3 in my case) matches the sun-compat version (1.6.0.03) then you are done and can do any clean-up (see below). If you type java -version at a prompt, the version number should be as expected. Otherwise...


Creating your own sun-compat RPM (where sun-compat <> JDK)
Well, you were eager and could not wait for the jpackage guys to create a java-1.6.0-sun-compat-1.6.0.03-1jpp.i586.rpm for you. You just want the latest JDK working. Sound like me. No problems. You have already download the source file (java-1.6.0-sun-compat-1.6.0.02-1jpp.src.rpm or similar) so you can make your own. You have also installed it (3rd bullet above).

To make your own sun-compat RPM just do the following:
  • cd to /usr/src/redhat/SPECS/ (the source directory may be different on other distros)
  • edit java-1.6.0-sun-compat.spec using your favorite text editor
  • At the top of the file you will see %define buildver 02
    • For this example we want 6u3 so buildver should be changed from "02" to "03".
  • Save and close the file.
  • Make your RPM
    • command: rpmbuild -ba java-1.6.0-sun-compat.spec
    • stuff happens...
  • Look in /usr/src/redhat/RPMS/i586/ and you will see java-1.6.0-sun-compat-1.6.0.03-1jpp.i586.rpm
  • install it
    • command: rpm -Uvh java-1.6.0-sun-compat-1.6.0.03-1jpp.i586.rpm
As above, you should be able to run java -version and see (as in my case)

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)


That is it.


Clean-Up After Successfull Install/Upgrade
Once you are sure everything is using the new version, you can (if desired) rpm -e your old jdk. Since SUN JDK 6 creates a symlink from the current version as "latest" there isn't much risk of using the old version... I did say "much" risk. I prefer removing the old once I confirm the new one is safe and stable.


Netbeans
To tell programs like Netbeans (we are all using it after all) to use the new JDK, simply update the JDK version in $NETBEANS_HOME/etc/netbeans.conf where $NETBEANS_HOME is the location of the netbeans install. That will affect all users of that install globally.


Java WebStart
I love WebStart, however, for some odd reason, desktop shortcuts created for linux (GNOME) hard code the full path (including version) to the javaws program. This is stupid since they should really just point to "latest". Regardless, you can either de-install/re-install the Java WebStart apps, or do the lazy approach and just edit the *.desktop files in your ~/Desktop/ directory. Just update the JDK version to match the current version.


Final Thoughts
This may seem like a long process but the steps are basic and easy. I keep the sun-compat source installed on my system and update the version number each time a new JDK comes out. I only get a new src and i586 version for new major JDK releases (i.e. JDK 5 to JDK 6). This makes the update process take a few minutes. About the same time it takes in Windows to update the PATH of the System Environment.

I hope these steps help it become easier for you too. I also hope that this blog become obsolete and that linux distros start bundling real SUN Java by default. It just the right thing to do. Some distros are already doing this. Fedora? Do you hear this? :-)

BTW
For Ubuntu, this may work. I need to try it.
http://coffeecokeandcode.blogspot.com/2008/09/installing-java-6-update-10-on-ubuntu.html

2 comments:

Anonymous said...

Great!!

I always want to get rid of gcj

Pedro Garcia InĂ¡cio said...

Thanks. Saved a lot of worries! :-D