3 # Arch Linux helper script to set/unset/check/fix the enabled Java environment
4 # This program may be freely redistributed under the terms of the GNU General Public License
6 # Author: Guillaume ALAUX <guillaume@archlinux.org>
10 DEFAULT_PATH
=${JVM_DIR}/${DEFAULT_NAME}
11 DEFAULT_NAME_JRE
=default-runtime
12 DEFAULT_PATH_JRE
=${JVM_DIR}/${DEFAULT_NAME_JRE}
18 if [ $
(id
-u) -ne 0 ]; then
19 echo 'This script must be run as root'
24 # $1: parameter count given to this script for this option
25 # $2: expected parameter count for this option
27 if [ $1 -ne $2 ]; then
28 echo 'Wrong parameter count'
33 # Second level functions
36 path
=$
(readlink
-e ${DEFAULT_PATH})
37 if [ "x${path}" != "x/dev/null" ]; then
38 echo ${path/${JVM_DIR}\/}
44 get_installed_javas
() {
45 if [ -d ${JVM_DIR} ]; then
46 for dir
in $
(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d |
sort); do
47 if [ -x ${dir}/bin
/java
]; then
48 javas
+=(${dir/${JVM_DIR}\/})
50 if [ -x ${dir}/jre
/bin
/java
]; then
51 javas
+=(${dir/${JVM_DIR}\/}/jre
)
59 # $1: Java environment name to test
61 test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin
/java
64 # $1: Java environment name to set as default
65 set_default_link_to
() {
67 unlink
${DEFAULT_PATH} 2>/dev
/null
68 ln -sf ${new_default} ${DEFAULT_PATH}
70 unlink
${DEFAULT_PATH_JRE} 2>/dev
/null
71 if [[ -d ${new_default}/jre
]]; then
72 ln -sf ${new_default}/jre
${DEFAULT_PATH_JRE}
74 ln -sf ${new_default} ${DEFAULT_PATH_JRE}
78 unset_default_link
() {
79 unlink
${DEFAULT_PATH} 2>/dev
/null
80 unlink
${DEFAULT_PATH_JRE} 2>/dev
/null
83 # First level functions
86 installed_java
=($
(get_installed_javas
))
87 if [ ${#installed_java[@]} -eq 0 ]; then
88 echo 'No compatible Java environment installed'
90 default_java
=$
(get_default_java
)
91 echo 'Available Java environments:'
92 for java
in ${installed_java[@]}; do
93 # We discoverd this Java env but its JRE is actually set as default
94 if [ "${java}/jre" = "${default_java}" ]; then
95 echo -e " ${java} (${java}/jre default)"
96 elif [ ${java} = "${default_java}" ]; then
97 echo -e " ${java} (default)"
102 if [ -z ${default_java} ]; then
103 echo "No Java environment set as default"
112 # $1: Java environment name to set as default
114 if ! is_java_valid
$1; then
115 echo "'${JVM_DIR}/$1' is not a valid Java environment path"
118 default
=$
(get_default_java
)
119 if [ "x$1" != "x${default}" ] ||
! is_java_valid
${default}; then
121 set_default_link_to
$1
124 #parent_dir=$(dirname $1)
125 #if is_java_valid ${parent_dir}; then
126 # echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default"
127 # echo "Fix this with 'archlinux-java set ${parent_dir}'"
131 # $1: Java environment name to unset
137 default
=$
(get_default_java
)
138 if is_java_valid
${default}; then
139 # If its parent is also a valid Java environment
140 if is_java_valid $
(dirname ${default}); then
142 set_default_link_to $
(dirname ${default})
145 prev
=$
(readlink
${DEFAULT_PATH})
147 potential_fixes
=("${prev/\/jre}" "${prev}/jre")
148 openjdk8
=('java-8-openjdk' 'java-8-openjdk/jre')
149 # List of environments to check by order of preference:
150 # - first potential fixes of user choices,
151 # - then OpenJDK8 as it is considered a default in Arch Linux
152 # - finally, any installed environment
153 to_check
=(${potential_fixes[@]} ${openjdk8[@]} $
(get_installed_javas
))
154 for java
in ${to_check[@]}; do
155 if ! is_java_valid $
(get_default_java
) && is_java_valid
${java}; then
156 set_default_link_to
${java}
160 if ! is_java_valid $
(get_default_java
); then
161 echo 'No valid Java environment found'
166 echo "$(basename $0) <COMMAND>"
168 echo -e '\tstatus\t\tList installed Java environments and enabled one'
169 echo -e '\tget\t\tReturn the short name of the Java environment set as default'
170 echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default'
171 echo -e '\tunset\t\tUnset current default Java environment'
172 echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration'
177 'status') do_status
;;
179 'set') check_root
; check_param_count
$# 2; do_set
$2;;
180 'unset') check_root
; do_unset
;;
181 'fix') check_root
; do_fix
;;
182 'help' |
'--help' |
'-h' |
'') usage
;;
183 *) echo "$(basename $0): unknown option '$@'"; exit 1;;