[fedora-java] quick and dirty Expect-fu to help manage "java" and "javac" alternatives

Vadim Nasardinov vadimn at redhat.com
Fri Jul 22 22:10:45 UTC 2005


There is a mildly annoying gotcha in the way FC4 manages the "java"
and "javac" alternatives.  (To be fair, FC4 is merely following the
JPackage suit here.)

The way it works is this.  If you install another JDK in addition to
GCJ, you have a choice of specifying your preferred alternative
systemwide like so:

 | $ readlink -f /usr/bin/java
 | /usr/bin/gij
 | $ sudo /usr/sbin/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.4.2-sun/bin/java
 | 
 | Enter to keep the current selection[+], or type selection number: 2
 | $ readlink -f /usr/bin/java
 | /usr/lib/jvm/java-1.4.2-sun-1.4.2.08/jre/bin/java

Here's the rub.  If you do this, you must remember to also change the
"javac" alternative.  (In some cases, you may not care if your
/usr/bin/java and /usr/bin/javac are provided by different vendors.
In most cases, you probably want to be consistent.)

I wrote a script to change both "java" and "javac" alternatives in one
fell swoop.  Here's how it works:

 | $ sudo ./alt-java
 | spawn /usr/sbin/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.4.2-sun/bin/java
 | 
 | Enter to keep the current selection[+], or type selection number: 1
 | 1
 | spawn /usr/sbin/alternatives --config javac
 | 
 | There are 2 programs which provide 'javac'.
 | 
 |   Selection    Command
 | -----------------------------------------------
 |    1           /usr/lib/jvm/java-1.4.2-gcj/bin/javac
 | *+ 2           /usr/lib/jvm/java-1.4.2-sun/bin/javac
 | 
 | Enter to keep the current selection[+], or type selection number: 1


I am attaching the script in hopes that someone else will find it useful.

------------------------------------------------------------------------------



-------------- next part --------------
#!/usr/bin/expect
# -*- mode: tcl -*-

# Author:  Vadim Nasardinov (vadimn at redhat.com)
# Since:   2005-07-22

spawn /usr/sbin/alternatives --config java

set prompt {Enter to keep the current selection[+], or type selection number:}

proc die {} {
    puts "timed out"
    exec kill [exp_pid]
    exit 1
}

expect {
    -ex $prompt {
        expect_user -re "(.*)\n"
        set selection $expect_out(1,string)
        send "$selection\r"
        # If we get nonsensical input from the user, the alternatives
        # system will redisplay the same prompt.  In this case, we
        # loop.
        exp_continue
    } timeout die
}

# If "java" is unknown to the alternatives system, the above command
# exits silently and the "selection" variable remains unset.

if {![info exists selection]} {
    puts {"java" is not managed by alternatives}
    exit 1
}

set client_exit [wait]
set os_code [lindex $client_exit 2]
set client_retc [lindex $client_exit 3]

if {$os_code != 0} {
    puts "OS error"
    exit 1
}

if {$client_retc != 0} {
    puts "something went wrong"
    exit $client_retc
}

spawn /usr/sbin/alternatives --config javac

expect {
    -ex $prompt {
        # FIXME: For now, we assume that "java" and "javac" are
        # numbered consistently. For example, if Sun's "java" appears
        # as 1 in the list of "java" alternatives, then Sun's "javac"
        # also appears under 1 in the list of "javac" alternatives.
        send "$selection\r"
    } timeout die
}

wait

puts $selection



More information about the fedora-devel-java-list mailing list