Skip to content

Instantly share code, notes, and snippets.

@cwchien
Last active January 6, 2023 19:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cwchien/1114571 to your computer and use it in GitHub Desktop.
Save cwchien/1114571 to your computer and use it in GitHub Desktop.
script for registering JDK7 to update-alternatives in Debian's way
#!/bin/bash
# "Register JDK7 for update-alternatives Script in Debian Way"
# Based on 1. codeslinger's "install_java7_alternatives" from https://gist.github.com/445930
# 2. sun-java6-plugin.deb postint script (for plugin)
# Modified by Cheng-Wei Chien<e.cwchien@gmail.com>, 2012-08-16
# ------------------------------------------------------------------------------
# Note: This script works on x86/amd64 Debian GNU/Linux|Ubuntu.
# If you use other arch or distro, you may have to modify some parameters.
#
# Usage: 1. Extract jdk7 tar.gz to, for example, /usr/lib/jvm/java-7-sun-1.7.0.13,
# create a symbolic link /usr/lib/jvm/java-7-sun to it.
# 2. Download java-7-sun.jinfo from https://gist.github.com/1114515,
# move it to /usr/lib/jvm/.java-7-sun.jinfo, do some modification if necessary.
# 3. Run this script with sudo
# 4. sudo update-java-alternatives -s java-7-sun
# 5. Have fun with jdk7!
#
# Trouble Shooting: If something goes wrong,
# 1. Uncomment "remove" commands and comment "install" commands.
# 2. Run this script again to rollback.
# 3. Fix what causes the problem, maybe typo or something else.
# 4. Comment "remove" commands and uncomment "install" commands
# 5. Run this script to update
# Force the shell to exit immediately if something goes wrong
set -e
# Follow the Debian's priority rule for java5 = 53, java6 = 63 .. I guess
priority=73
# Your jdk7 home, in this case, it's /usr/lib/jvm/java-7-sun-1.7.0.13
# But I create a symbolic link /usr/lib/jvm/java-7-sun to meet Debian's way
basedir=/usr/lib/jvm/java-7-sun
# Your man1 dir, you don't need to change this
mandir=$basedir/man/man1
# Variables from /usr/lib/jvm/.java-6-sun.jinfo except HtmlConverter (not in jdk7)
jdk_var='appletviewer apt extcheck idlj jar jarsigner java-rmi.cgi javac javadoc javafxpackager javah javap jconsole jdb jhat jinfo jmap jps jrunscript jsadebugd jstack jstat jstatd native2ascii rmic schemagen serialver wsgen wsimport xjc'
jre_var='ControlPanel java java_vm javaws jcontrol keytool pack200 policytool rmid rmiregistry unpack200 orbd servertool tnameserv jexec'
browser_var="xulrunner-addons firefox iceape iceweasel mozilla midbrowser xulrunner"
# Java plugin path, if your machine is x86, remeber to replace "amd64" with "i386"
plugin=$basedir/jre/lib/amd64/libnpjp2.so
# First, gzip man1 files to meet the 1.gz form
files=$(ls $mandir/*.1 2> /dev/null | wc -l)
if [ "$files" != "0" ]; then
for i in $mandir/*.1; do
gzip $i
done
fi
# Register jdk variables
for i in $jdk_var; do
unset slave || true
if [ -e $mandir/$i.1.gz ]; then
slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
fi
#update-alternatives --remove $i $basedir/bin/$i
update-alternatives --install /usr/bin/$i $i $basedir/bin/$i $priority $slave
done
# Register jre variables
for i in $jre_var; do
unset slave || true
if [ -e $mandir/$i.1.gz ]; then
slave="--slave /usr/share/man/man1/$i.1.gz $i.1.gz $mandir/$i.1.gz"
fi
if [ $i = jexec ]; then
#update-alternatives --remove jexec $basedir/jre/lib/jexec
update-alternatives --install /usr/bin/jexec jexec $basedir/jre/lib/jexec $priority
else
#update-alternatives --remove $i $basedir/jre/bin/$i
update-alternatives --install /usr/bin/$i $i $basedir/jre/bin/$i $priority $slave
fi
done
# Register plugins
for i in $browser_var; do
if [ $i = xulrunner-addons ]; then
browser=xulrunner-1.9
else
browser=$i
fi
#update-alternatives --remove $browser-javaplugin.so $plugin
update-alternatives --install /usr/lib/$i/plugins/libjavaplugin.so $browser-javaplugin.so $plugin $priority
done
@bauna
Copy link

bauna commented Aug 15, 2011

For installing on Ubuntu 64 bits, you should replace all 'i386' by 'amd64' on this script and /usr/lib/jvm/.java-7-sun.jinfo
It would be a nice enhancement that the script detects the platform ;-)

@jan0x6c
Copy link

jan0x6c commented Oct 5, 2011

I have problem with (line 47): if [ -e $mandir/*.1 ]; then

register_jdk7_for_update_alternatives.sh: line 48: [: too many arguments

idea:
http://www.ducea.com/2009/03/05/bash-tips-if-e-wildcard-file-check-too-many-arguments/

solution:

First, gzip man1 files to meet the 1.gz form

files=$(ls $mandir/.1 2> /dev/null | wc -l)
if [ "$files" != "0" ]; then
for i in $mandir/
.1; do
gzip $i
done
fi

@cwchien
Copy link
Author

cwchien commented Oct 6, 2011

thanks for your solution.

I've updated the script.

@cwchien
Copy link
Author

cwchien commented Jun 25, 2012 via email

@cwchien
Copy link
Author

cwchien commented Aug 15, 2012

2012-08-16 edit: modify line 39 add "javafxpackager" just after "javadoc" because Oracle JDK 7u6 includes JavaFX now.

Note: if you also use my .java-7-sun.jinfo (https://gist.github.com/1114515), please update it, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment