Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'alternatives' really better for managing JDKs than a Symlink and $PATH?

I've just recently upgraded to Fedora 16 (from fedora 12), and have read/been told that instead of setting up different JDKs by using a simple symlink and setting my $PATH to that symlink, I should be using the alternatives tool.

What I don't understand is how alternatives is better for managing your jdk, when it seems you would have to run: alternatives --config not only for 'java' but also all the supporting tools (javac, javaws, jstack, etc). This seems miserable compared to:

(Assume $PATH=/opt/local/java/current/bin:...)

rm /opt/local/java/current
ln -s /path/to/unpacked/jdkX /opt/local/java/current

So my question is:

Why do I hear alternatives is the proper way to manage java tools in newer versions of Fedora when it seems much more cumbersome to fully switch JDK's? Have I just been told poor information, or am I missing something important about alternatives?

(NOTE: Feel free to be brutal if alternatives is clearly better in some way. I'm aware I'm largely ignorant about the tool)

like image 506
Barryrowe Avatar asked May 11 '12 14:05

Barryrowe


People also ask

What is update-alternatives priority?

From man update-alternatives : Each alternative has a priority associated with it. When a link group is in automatic mode, the alternatives pointed to by members of the group will be those which have the highest priority.

What is update-alternatives in Linux?

update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system. It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time.

What is update Java alternatives?

update-java-alternatives is a program to update alternatives for jre/jdk installations. update-alternatives is a symbolic link management system for linux (I'm sure there is little news here). You can, and really should, use both update-java-alternatives and update-alternatives together.


1 Answers

General

If you know you just need to swap out one or two tools (ex: java and javac), alternatives seems like the way to go, as it is the intended way to manage application versions.

However, if you are using multiple development tools that may require a JAVA_HOME or JDK_HOME value set, or if you don't know which of the jdk utilities the tool(s) may call, it seems exporting your jdk path to $JAVA_HOME, and prepending it to $PATH is a simpler way to go. It may not be the "correct" way, but it's quicker to switch between java versions, and more transparent as you know all your jdk utilities will be pointed at the same version.

  1. Unzip your new jdk to your normal java location (/opt/local/java/jdk_1.X_XX)
  2. Symlink your current jdk

    ln -s /opt/local/java/jdk_1.X_XX current
    
  3. In ~/.bash_profile OR ~/.bashrc add

    JAVA_HOME=/opt/local/java/current
    export JAVA_HOME
    JDK_HOME=$JAVA_HOME
    export JDK_HOME
    
    PATH=$JAVA_HOME/bin:$PATH
    export PATH
    
  4. Now if you need to switch jdks you can just swap the symlink

    rm /opt/local/java/current
    ln -s /opt/local/java/new_jdk_directory current
    

Ubuntu Specific

It appears that on UBUNTU this problem has been solved with update-java-alternatives which will update all of the alternatives for a given runtime or development kit (JRE/JDK).

like image 154
Barryrowe Avatar answered Oct 04 '22 02:10

Barryrowe