Thursday, July 19, 2012

Tip: Installing JDK RPM on linux and actaully using it


Oracle puts out JDK updates every now and then. The Linux update comes in the binary form (for install in any directory) and RPM form. I like RPM format because it plugs into OS's standard package installation mechanism. So I downloaded the RPM from here and installed it using:
> chmod u+x jdk-6u31-linux-x64-rpm.bin
> ./jdk-6u31-linux-x64-rpm.bin
:
:
:
and went to command line and typed:
> java -version 
java version "1.4.2"
gij (GNU libgcj) version 4.1.2 20080704 (Red Hat 4.1.2-48)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Huh? I had presumed that installing the RPM will do the right thing. Uh uh.
Sure enough:
> ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 Apr 13 11:35 /usr/bin/java -> /etc/alternatives/java
> ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 30 Apr 13 14:13 /etc/alternatives/java -> /usr/lib/jvm/jre-1.4.2-gcj/bin/java
revealed that java command was linked to  jre-1.4.2-gcj java.
Apparently you have to run the following commands to make the newly installed  java and javac as available alternatives.
> update-alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_31/bin/java 3000
> update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_31/bin/javac 3000




Now to see what all Java alternatives are available you can run:
> update-alternatives --display java
java - status is manual.
 link currently points to /usr/lib/jvm/jre-1.4.2-gcj/bin/java
/usr/lib/jvm/jre-1.4.2-gcj/bin/java - priority 1420
 slave keytool: /usr/lib/jvm/jre-1.4.2-gcj/bin/keytool
 slave rmiregistry: /usr/lib/jvm/jre-1.4.2-gcj/bin/rmiregistry
 slave jre_exports: /usr/lib/jvm-exports/jre-1.4.2-gcj
 slave jre: /usr/lib/jvm/jre-1.4.2-gcj
/usr/java/jdk1.6.0_31/bin/java - priority 3000
 slave keytool: (null)
 slave rmiregistry: (null)
 slave jre_exports: (null)
 slave jre: (null)
Current `best' version is /usr/java/jdk1.6.0_31/bin/java.




And finally to select which java to use - use the command:
> update-alternatives --config java
There are 2 programs which provide 'java'.
  Selection    Command
-----------------------------------------------
 + 1           /usr/lib/jvm/jre-1.4.2-gcj/bin/java
*  2           /usr/java/jdk1.6.0_31/bin/java
Enter to keep the current selection[+], or type selection number: 2 


Select the one from the list and type ENTER. Then do:
> update-alternatives --config javac
:
:

and viola! Now the system will use the Java you selected.
Hope this helps you!

2 comments:

Unknown said...

I hope this is applicable only incase of installing the java rpm.

If we use jdk-6u39-linux-x64.bin in this case it only extracts and not really an rpm install ,
In this case we need to set the JAVA_HOME and add $JAVA_HOME/bin dir to PATH variable to use it.

Is there any other effective way

Regards
Saravanan

kumar said...

Nice article