How to override default OpenJDK in Fedora with SUN/Oracle JDK

I always did it pretty straight, I installed SUN JDK, I put-in SUN’s “java” executable into PATH variable, “export JAVA_HOME”, I did it usually in some “*.sh” file in to “/etc/profile.d”, and well actually it is not a good place for it. However, Fedora manages mapping between “java” link and real target executable via system of symbolic links (managed via “alternatives” utility). Alas, SUN’s JDK does not “invoke alternatives” to declare its alternatives.
But today I had a time to solve this problem as it must have been solved:
Meet “add-java.sh”:

#!/bin/bash
JAVA_HOME="$1"
JAVA_HOME=`readlink -f "$JAVA_HOME"`
PRIORITY=1000
if [ -n "$2" ]; then
	PRIORITY="$2"
fi
alternatives --verbose --install /usr/bin/java java "${JAVA_HOME}/bin/java" ${PRIORITY} \
	--slave /usr/bin/javaws javaws "${JAVA_HOME}/bin/javaws" \
	--slave /usr/bin/keytool keytool "${JAVA_HOME}/bin/keytool" \
	--slave /usr/bin/orbd orbd "${JAVA_HOME}/bin/orbd" \
	--slave /usr/bin/pack200 pack200 "${JAVA_HOME}/bin/pack200" \
	--slave /usr/bin/rmid rmid "${JAVA_HOME}/bin/rmid" \
	--slave /usr/bin/rmiregistry rmiregistry "${JAVA_HOME}/bin/rmiregistry" \
	--slave /usr/bin/servertool servertool "${JAVA_HOME}/bin/servertool" \
	--slave /usr/bin/tnameserv orbd "${JAVA_HOME}/bin/tnameserv" \
	--slave /usr/bin/unpack200 unpack200 "${JAVA_HOME}/bin/unpack200" \
	--slave /usr/lib/jvm-exports/jre jre_exports "${JAVA_HOME}/jre" \
	--slave /usr/lib/jvm/jre jre "${JAVA_HOME}/jre" \
	--slave /usr/share/man/man1/java.1 java.1 "${JAVA_HOME}/man/man1/java.1" \
	--slave /usr/share/man/man1/javaws.1 javaws.1 "${JAVA_HOME}/man/man1/javaws.1" \
	--slave /usr/share/man/man1/keytool.1 keytool.1 "${JAVA_HOME}/man/man1/keytool.1" \
	--slave /usr/share/man/man1/orbd.1 orbd.1 "${JAVA_HOME}/man/man1/orbd.1" \
	--slave /usr/share/man/man1/pack200.1 pack200.1 "${JAVA_HOME}/man/man1/pack200.1" \
	--slave /usr/share/man/man1/rmid.1 rmid.1 "${JAVA_HOME}/man/man1/rmid.1" \
	--slave /usr/share/man/man1/rmiregistry.1 rmiregistry.1 "${JAVA_HOME}/man/man1/rmiregistry.1" \
	--slave /usr/share/man/man1/servertool.1 servertool.1 "${JAVA_HOME}/man/man1/servertool.1" \
	--slave /usr/share/man/man1/tnameserv.1 tnameserv.1 "${JAVA_HOME}/man/man1/tnameserv.1" \
	--slave /usr/share/man/man1/unpack200.1 unpack200.1 "${JAVA_HOME}/man/man1/unpack200.1"

Run it under root:

./add-java.sh /usr/java/latest

NOTE: /usr/java/latest is what would be JAVA_HOME if you selected “classical” approach.
And now you may run (again as root):

# alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
   2           /usr/java/latest/bin/java

Enter to keep the current selection[+], or type selection number: 

Select 2 and it will result in:

# java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode)

If you rerun alternatives --config java and select 1 you will return to the previous configuration:

# java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.7) (fedora-52.1.9.7.fc14-i386)
OpenJDK Server VM (build 19.0-b09, mixed mode)

The advantage is that such a toggle is immediate, you should not relogin to force files from profile.d to run.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: