Saturday, March 15, 2008

Using 'Alternatives' in Linux to use a different Java package

Installing Sun Java alongside the default GNU java using the 'alternatives' system.
This post shows how to install Sun's java implementation alongside the Linux default GNU java.

Installing Sun Java alongside the default GNU java

Sun's java isn't installed by default on RedHat systems. This is because Sun hasn't licensed it for RedHat's distribution. Instead, Linux ships with an open source alternative, GNU Java. It's based on Sun's Java implementation, and--in part because of that--it's always a version or two behind Sun. In all, it's a very good implementation, but sometimes--as in the case with DSpace--it's necessary to use Sun's Java, instead.
We don't want to remove the Gnu Java; we'll install Sun's alongside it.


Download the files

The first file we need is Sun's java implementation. In this example, we'll be referencing JDK 1.6.0 release 4, but the steps are the same for all versions. 
Links to Sun's most recent download packages can be found here.
The second file is a compatibility package from www.jpackage.org. This simply creates a bunch of symbolic links to bring Sun's directory locations into compliance with the GNU system.
We'll be referencing java-1.6.0-sun-compat-1.6.0.04-1jpp.i586.rpm, which works with the version of Sun's Java discussed above (1.6.0_4). Whichever version you're using, you'll need to visit the jpackage web site to download it.
JPackage has managed, somehow to make their downloads almost as inscrutable as Sun's. Their instructions page, though, has good pointers to all the correct downloads.
If you run into problems either finding the correct sun-compat package, or if you're running a distro (such as 64-bit RHEL) for which JPackage hasn't created a sun-compat package, you're not out of luck. We'll also look at how to configure alternatives manually, at least for basic java usage.

Install the Packages

Install Sun Java

Sun packages its distributions in a self-extracting binary file. Simply execute the .bin file from the command line:
./jdk-6u4-linux-i586-rpm.bin

This will take awhile. When it's finished, Sun's Java will be installed.
You can see, however, that the GNU java still is active by typing the following command
java -version

Configure Alternatives manually

If you've not found the correct JPackage sun-compat package, or if one isn't available, it's still quite possible to use the alternatives system to manage your java versions.
Alternatives is configured at the command line:
alternatives --install
alternatives --config
alternatives --remove

So, to get straight to the meat of the matter:
alternatives --install /usr/bin/java java /usr/java/jdk1.6.0_11/bin/java 120 \
--slave /usr/bin/keytool keytool /usr/java/jdk1.6.0_11/bin/keytool \
--slave /usr/bin/rmiregistry rmiregistry /usr/java/jdk1.6.0_11/bin/rmiregistry
alternatives --install /usr/bin/javac javac /usr/java/jdk1.6.0_11/bin/javac 120 \
--slave /usr/bin/jar  jar  /usr/java/jdk1.6.0_11/bin/jar \
--slave /usr/bin/rmic rmic /usr/java/jdk1.6.0_11/bin/rmic

The above will create two entries in the alternatives symlink config system, one for java (with some "slave" symlinks for dependent apps), and one for javac (likewise with slave symlinks).
By and large, the above should be what's necessary to run java.


Check the active Java Version
Now we need to verify that Sun's java is the default (using the java -version command):
$ java -version
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Server VM (build 10.0-b19, mixed mode)

Above, we see that Sun's Java v 1.6.0 r4 is the default java. If, however, we see something like the following,
# java -version
java version "1.4.2"
gij (GNU libgcj) version 4.1.2 20070626 (Red Hat 4.1.2-13)

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.

Then we have to use the alternatives program to set the correct default. Alternatives simply is a system that manages standard symbolic links, allowing you to select one or another alternative program to run for any given command. In our case, that's java.
As root, simply run the following command
alternatives --config java

you'll see something like the following:
# 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/lib/jvm/jre-1.6.0-sun/bin/java

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

The + sign next to the first line indicates that the GNU Compiler for Java (GCJ) is the default. Simply type the number 2 and press enter to make the Sun Java distro be the default.

Install Sun Campatibility

Jpackage has a Sun (and other) compatibility package that is designed to streamline the process. Frankly, I think it's harder to find the correct package than just to use alternatives to configure things manually.  However, here's how you do it, if you're so inclinded.
java-1.6.0-sun-compat-1.6.0.04-1jpp.i586.rpm is signed by jpackage.org, so the easiest way to install the package is to import the jpackage.org key:
rpm --import http://jpackage.org/jpackage.asc

If you don't want to do this (and thus trust all Jpackage.org rpm packages), you'll need to use RPM instead of yum to install it.
Then, simply install the package:
yum install java-1.6.0-sun-compat-1.6.0.04-1jpp.i586.rpm

17 comments:

  1. Great work dude!

    ReplyDelete
  2. Thanks so much for this great explanation of how to get Java setup on Linux!

    ReplyDelete
  3. Thank you so much!!! I am a complete beginner to Linux. The alternatives method worked for me with Oracle Enterprise Linux, but for some reason I had to enter each "--slave..." line separately with its corresponding "--install.." command. (so in other words I entered four alternatives commands). Don't know why, but it seemed to work. Again, thank you so much.

    ReplyDelete
  4. CORRECTION TO MY PREVIOUS COMMENT: Regarding the above comment of Nov. 6, it wasn't really necessary for me to separate the "--slave" arguments. The problem was that I had accidentally picked up a Windows line break when I copied over the commands. When I removed the line break that wasn't supposed to be there it worked fine.

    ReplyDelete
  5. I was having the problem of :
    java version "1.4.2""
    even after installing the java from Sun's RPM.

    Your notes about alternatives made everything work perfect.
    T H A N K S !

    ReplyDelete
  6. Thanks a whole bunch! Saved the day :)

    ReplyDelete
  7. Thanks, It is very use full for me.

    ReplyDelete
  8. Hi, Mike,
    Thanks for asking that; I'm a little embarrassed that I didn't explain some of that before.

    The number after the command is the priority (IIRC, the default is 100), that alternatives uses to display and set the individual configurations.

    This site mentions it: http://linux.about.com/library/cmd/blcmdl8_alternatives.htm

    I do think I should add some explanation to this, especially as there isn't a lot of detailed info easily available out in the wild.

    Thanks again! -Lane

    ReplyDelete
  9. can we do it on test users rather than root user

    ReplyDelete
  10. Progress and developments since then have been fast and furious, and online games as they now were beyond imagination just 10 years ago.
    StartPoint

    ReplyDelete
  11. Obviously, you need technical support for fixing the issue. Formatting the computer or reloading your operating system can fix the problem. Techwitty

    ReplyDelete
  12. From following a friend to sharing location, from finding new places to accurately tracing location, all these things are possible with powerful apps.http://www.iphonetrackingapps.com/how-to-recover-deleted-snapchats-on-iphone/

    ReplyDelete
  13. Technical issues take place anytime anywhere, so the third party service provider you have hired should be ready 24x7 to resolve your technical issues. Darknet Market

    ReplyDelete
  14. Mike Wallace IT or technology support features always combine high precision ERP systems. This is why things get real easier and you feel it comfortable to handle tricky chores at ease. Consolidated operations are catered keeping parity with proper consulting recommendations.

    ReplyDelete
  15. Some folks may be having issues with the internet or some might have computer systems which are not working at all due to the major issues such as failure of operating system. http://www.techwarn.com/remove-riskware-ifeohijack-2-easy-steps-ultimate-guide/

    ReplyDelete
  16. Cell phone tracking apps will help you to keep an eye on your kids without touching their phones. Check this link for more information.

    ReplyDelete

Thanks for leaving a comment!