-
Notifications
You must be signed in to change notification settings - Fork 15
/
jdk_switcher.sh
executable file
·350 lines (305 loc) · 10.7 KB
/
jdk_switcher.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env bash
if uname -a | grep x86_64 >/dev/null; then
ARCH_SUFFIX=amd64
else
ARCH_SUFFIX=i386
fi
: "${JDK_SWITCHER_DEFAULT:=openjdk8}"
: "${UJA:=update-java-alternatives}"
OPENJDK6_UJA_ALIAS="java-1.6.0-openjdk"
OPENJDK6_JAVA_HOME="/usr/lib/jvm/java-6-openjdk"
if [[ -d "/usr/lib/jvm/java-6-openjdk-${ARCH_SUFFIX}" ]]; then
OPENJDK6_UJA_ALIAS="java-1.6.0-openjdk-${ARCH_SUFFIX}"
OPENJDK6_JAVA_HOME="/usr/lib/jvm/java-6-openjdk-${ARCH_SUFFIX}"
fi
OPENJDK7_UJA_ALIAS="java-1.7.0-openjdk"
OPENJDK7_JAVA_HOME="/usr/lib/jvm/java-7-openjdk"
if [[ -d "/usr/lib/jvm/java-7-openjdk-${ARCH_SUFFIX}" ]]; then
OPENJDK7_UJA_ALIAS="java-1.7.0-openjdk-${ARCH_SUFFIX}"
OPENJDK7_JAVA_HOME="/usr/lib/jvm/java-7-openjdk-${ARCH_SUFFIX}"
fi
OPENJDK8_UJA_ALIAS="java-1.8.0-openjdk"
OPENJDK8_JAVA_HOME="/usr/lib/jvm/java-8-openjdk"
if [[ -d "/usr/lib/jvm/java-8-openjdk-${ARCH_SUFFIX}" ]]; then
OPENJDK8_UJA_ALIAS="java-1.8.0-openjdk-${ARCH_SUFFIX}"
OPENJDK8_JAVA_HOME="/usr/lib/jvm/java-8-openjdk-${ARCH_SUFFIX}"
fi
ORACLEJDK7_UJA_ALIAS="java-7-oracle"
ORACLEJDK7_JAVA_HOME="/usr/lib/jvm/java-7-oracle"
if [[ -d "/usr/lib/jvm/java-7-oracle-${ARCH_SUFFIX}" ]]; then
ORACLEJDK7_UJA_ALIAS="java-7-oracle-${ARCH_SUFFIX}"
ORACLEJDK7_JAVA_HOME="/usr/lib/jvm/java-7-oracle-${ARCH_SUFFIX}"
fi
ORACLEJDK8_UJA_ALIAS="java-8-oracle"
ORACLEJDK8_JAVA_HOME="/usr/lib/jvm/java-8-oracle"
if [[ -d "/usr/lib/jvm/java-8-oracle-${ARCH_SUFFIX}" ]]; then
ORACLEJDK8_UJA_ALIAS="java-8-oracle-${ARCH_SUFFIX}"
ORACLEJDK8_JAVA_HOME="/usr/lib/jvm/java-8-oracle-${ARCH_SUFFIX}"
fi
ORACLEJDK9_UJA_ALIAS="java-9-oracle"
ORACLEJDK9_JAVA_HOME="/usr/lib/jvm/java-9-oracle"
if [[ -d "/usr/lib/jvm/java-9-oracle-${ARCH_SUFFIX}" ]]; then
ORACLEJDK9_UJA_ALIAS="java-9-oracle-${ARCH_SUFFIX}"
ORACLEJDK9_JAVA_HOME="/usr/lib/jvm/java-9-oracle-${ARCH_SUFFIX}"
fi
ORACLEJDK10_UJA_ALIAS="java-10-oracle"
ORACLEJDK10_JAVA_HOME="/usr/lib/jvm/java-10-oracle"
if [[ -d "/usr/lib/jvm/java-10-oracle-${ARCH_SUFFIX}" ]]; then
ORACLEJDK10_UJA_ALIAS="java-10-oracle-${ARCH_SUFFIX}"
ORACLEJDK10_JAVA_HOME="/usr/lib/jvm/java-10-oracle-${ARCH_SUFFIX}"
fi
IBMJAVA8_UJA_ALIAS="java-8-ibm"
IBMJAVA8_JAVA_HOME="/usr/lib/jvm/java-8-ibm"
if [[ -d "/usr/lib/jvm/java-8-ibm-${ARCH_SUFFIX}" ]]; then
IBMJAVA8_UJA_ALIAS="java-8-ibm-${ARCH_SUFFIX}"
IBMJAVA8_JAVA_HOME="/usr/lib/jvm/java-8-ibm-${ARCH_SUFFIX}"
fi
OPENJDK8_OPENJ9_UJA_ALIAS="openjdk8-openj9"
OPENJDK8_OPENJ9_JAVA_HOME="/usr/lib/jvm/openjdk8-openj9"
if [[ -d "/usr/lib/jvm/openjdk8-openj9-${ARCH_SUFFIX}" ]]; then
OPENJDK8_OPENJ9_UJA_ALIAS="openjdk8-openj9-${ARCH_SUFFIX}"
OPENJDK8_OPENJ9_JAVA_HOME="/usr/lib/jvm/openjdk8-openj9-${ARCH_SUFFIX}"
fi
for config_file in /etc/default/jdk-switcher "${HOME}/.jdk_switcherrc" "${JDK_SWITCHER_CONFIG}"; do
if [[ -f "${config_file}" ]]; then
# shellcheck source=/dev/null
source "${config_file}"
fi
done
switch_to_openjdk6() {
echo "Switching to OpenJDK6 ($OPENJDK6_UJA_ALIAS), JAVA_HOME will be set to $OPENJDK6_JAVA_HOME"
sudo "${UJA}" --set "$OPENJDK6_UJA_ALIAS"
export JAVA_HOME="$OPENJDK6_JAVA_HOME"
}
switch_to_openjdk7() {
echo "Switching to OpenJDK7 ($OPENJDK7_UJA_ALIAS), JAVA_HOME will be set to $OPENJDK7_JAVA_HOME"
sudo "${UJA}" --set "$OPENJDK7_UJA_ALIAS"
export JAVA_HOME="$OPENJDK7_JAVA_HOME"
}
switch_to_openjdk8() {
echo "Switching to OpenJDK8 ($OPENJDK8_UJA_ALIAS), JAVA_HOME will be set to $OPENJDK8_JAVA_HOME"
sudo "${UJA}" --set "$OPENJDK8_UJA_ALIAS"
export JAVA_HOME="$OPENJDK8_JAVA_HOME"
}
switch_to_oraclejdk7() {
echo "Switching to Oracle JDK7 ($ORACLEJDK7_UJA_ALIAS), JAVA_HOME will be set to $ORACLEJDK7_JAVA_HOME"
sudo "${UJA}" --set "$ORACLEJDK7_UJA_ALIAS"
export JAVA_HOME="$ORACLEJDK7_JAVA_HOME"
}
switch_to_oraclejdk8() {
echo "Switching to Oracle JDK8 ($ORACLEJDK8_UJA_ALIAS), JAVA_HOME will be set to $ORACLEJDK8_JAVA_HOME"
sudo "${UJA}" --set "$ORACLEJDK8_UJA_ALIAS"
export JAVA_HOME="$ORACLEJDK8_JAVA_HOME"
}
switch_to_oraclejdk9() {
echo "Switching to Oracle JDK9 ($ORACLEJDK9_UJA_ALIAS), JAVA_HOME will be set to $ORACLEJDK9_JAVA_HOME"
sudo "${UJA}" --set "$ORACLEJDK9_UJA_ALIAS"
export JAVA_HOME="$ORACLEJDK9_JAVA_HOME"
}
switch_to_oraclejdk10() {
echo "Switching to Oracle JDK10 ($ORACLEJDK10_UJA_ALIAS), JAVA_HOME will be set to $ORACLEJDK10_JAVA_HOME"
sudo "${UJA}" --set "$ORACLEJDK10_UJA_ALIAS"
export JAVA_HOME="$ORACLEJDK10_JAVA_HOME"
}
switch_to_ibmjava8() {
echo "Switching to IBM JAVA8 ($IBMJAVA8_UJA_ALIAS), JAVA_HOME will be set to $IBMJAVA8_JAVA_HOME"
update_jdk_entries $IBMJAVA8_JAVA_HOME
}
switch_to_openjdk8_openj9() {
echo "Switching to OPENJDK8 OPENJ9 ($OPENJDK8_OPENJ9_UJA_ALIAS), JAVA_HOME will be set to $OPENJDK8_OPENJ9_JAVA_HOME"
update_jdk_entries $OPENJDK8_OPENJ9_JAVA_HOME
}
update_jdk_entries() {
java_home="$1"
jre=0
dir="$java_home/jre/bin"
if [ -d "$dir" ]; then
jre_entries=("$(ls "$dir")")
jre=1
update_alternatives "$dir" "jre_entries[@]"
fi
dir="$java_home/bin"
if [ -d "$dir" ]; then
jdk_entries=("$(ls "$dir")")
# If jre dir is present then remove duplicate binaries from bin dir
if [ $jre == 1 ]; then
unique_jdk_entries=()
for jdk_entry in "${jdk_entries[@]}"; do
found=0
for jre_entry in "${jre_entries[@]}"; do
if [ "$jdk_entry" == "$jre_entry" ]; then
found=1
break
fi
done
if [ $found == 0 ]; then
# shellcheck disable=SC2034
unique_jdk_entries[i]=$jdk_entry
i=$((i + 1))
fi
done
update_alternatives "$dir" "unique_jdk_entries[@]"
else
update_alternatives "$dir" "jdk_entries[@]"
fi
fi
export JAVA_HOME="$java_home"
}
update_alternatives() {
dir="$1"
priority=1
binaries=("${!2}")
for binary in "${binaries[@]}"; do
path="$dir/$binary"
if [[ "$binary" == "classic" ]] || [[ "$binary" == "j9vm" ]]; then
continue
fi
sudo update-alternatives --quiet --install "/usr/bin/$binary" "$binary" "$path" "$priority"
sudo update-alternatives --quiet --set "$binary" "$path"
done
}
print_home_of_openjdk6() {
echo "$OPENJDK6_JAVA_HOME"
}
print_home_of_openjdk7() {
echo "$OPENJDK7_JAVA_HOME"
}
print_home_of_openjdk8() {
echo "$OPENJDK8_JAVA_HOME"
}
print_home_of_oraclejdk7() {
echo "$ORACLEJDK7_JAVA_HOME"
}
print_home_of_oraclejdk8() {
echo "$ORACLEJDK8_JAVA_HOME"
}
print_home_of_oraclejdk9() {
echo "$ORACLEJDK9_JAVA_HOME"
}
print_home_of_oraclejdk10() {
echo "$ORACLEJDK10_JAVA_HOME"
}
print_home_of_ibmjava8() {
echo "$IBMJAVA8_JAVA_HOME"
}
print_home_of_openjdk8_openj9() {
echo "$OPENJDK8_OPENJ9_JAVA_HOME"
}
warn_sunjdk6_eol() {
echo "Sun/Oracle JDK 6 is EOL since November 2012, and is no longer supported. Please consider upgrading..." >&2
}
warn_jdk_not_known() {
echo "Sorry, but JDK '$1' is not known." >&2
}
warn_gcj_user() {
echo "We do not support GCJ. I mean, come on. Are you Richard Stallman?" >&2
}
switch_jdk() {
case "${1:-default}" in
*gcj*)
warn_gcj_user
false
;;
openjdk6 | openjdk1.6 | openjdk1.6.0 | jdk6 | 1.6.0 | 1.6 | 6.0)
switch_to_openjdk6
;;
openjdk7 | jdk7 | 1.7.0 | 1.7 | 7.0)
switch_to_openjdk7
;;
openjdk8 | jdk8 | 1.8.0 | 1.8 | 8.0)
switch_to_openjdk8
;;
oraclejdk6 | oraclejdk1.6 | oraclejdk1.6.0 | oracle6 | oracle1.6.0 | oracle6.0 | sunjdk6 | sun6)
warn_sunjdk6_eol
false
;;
oraclejdk7 | oraclejdk1.7 | oraclejdk1.7.0 | oracle7 | oracle1.7.0 | oracle7.0 | oracle | sunjdk7 | sun7 | sun)
switch_to_oraclejdk7
;;
oraclejdk8 | oraclejdk1.8 | oraclejdk1.8.0 | oracle8 | oracle1.8.0 | oracle8.0)
switch_to_oraclejdk8
;;
oraclejdk9 | oraclejdk1.9 | oraclejdk1.9.0 | oracle9 | oracle1.9.0 | oracle9.0)
switch_to_oraclejdk9
;;
oraclejdk10 | oraclejdk1.10 | oraclejdk1.10.0 | oracle10 | oracle1.10.0 | oracle10.0)
switch_to_oraclejdk10
;;
ibmjava8 | ibmjava1.8 | ibmjava1.8.0 | ibmjdk8 | ibmjdk1.8 | ibmjdk1.8.0 | ibm8 | ibm1.8.0 | ibm8.0)
switch_to_ibmjava8
;;
openjdk8j9 | openjdk8openj9 | openjdk8withopenj9)
switch_to_openjdk8_openj9
;;
default)
"switch_to_${JDK_SWITCHER_DEFAULT}"
;;
*)
warn_jdk_not_known "$1"
false
;;
esac
}
print_java_home() {
typeset JDK
JDK="$1"
case "$JDK" in
*gcj*)
warn_gcj_user
;;
openjdk6 | openjdk1.6 | openjdk1.6.0 | jdk6 | 1.6.0 | 1.6 | 6.0)
print_home_of_openjdk6
;;
openjdk7 | jdk7 | 1.7.0 | 1.7 | 7.0)
print_home_of_openjdk7
;;
openjdk8 | jdk8 | 1.8.0 | 1.8 | 8.0)
print_home_of_openjdk8
;;
oraclejdk6 | oraclejdk1.6 | oraclejdk1.6.0 | oracle6 | oracle1.6.0 | oracle6.0 | sunjdk6 | sun6)
warn_sunjdk6_eol
;;
oraclejdk7 | oraclejdk1.7 | oraclejdk1.7.0 | oracle7 | oracle1.7.0 | oracle7.0 | oracle | sunjdk7 | sun7 | sun)
print_home_of_oraclejdk7
;;
oraclejdk8 | oraclejdk1.8 | oraclejdk1.8.0 | oracle8 | oracle1.8.0 | oracle8.0)
print_home_of_oraclejdk8
;;
oraclejdk9 | oraclejdk1.9 | oraclejdk1.9.0 | oracle9 | oracle1.9.0 | oracle9.0)
print_home_of_oraclejdk9
;;
oraclejdk10 | oraclejdk1.10 | oraclejdk1.10.0 | oracle10 | oracle1.10.0 | oracle10.0)
print_home_of_oraclejdk10
;;
ibmjava8 | ibmjava1.8 | ibmjava1.8.0 | ibmjdk8 | ibmjdk1.8 | ibmjdk1.8.0 | ibm8 | ibm1.8.0 | ibm8.0)
print_home_of_ibmjava8
;;
openjdk8j9 | openjdk8openj9 | openjdk8withopenj9)
print_home_of_openjdk8_openj9
;;
default)
"print_home_of_${JDK_SWITCHER_DEFAULT}"
;;
*)
warn_jdk_not_known "$JDK"
;;
esac
}
jdk_switcher() {
typeset COMMAND JDK
COMMAND="$1"
JDK="$2"
case "$COMMAND" in
use)
switch_jdk "$JDK"
;;
home)
print_java_home "$JDK"
;;
*)
echo "Usage: jdk_switcher {use|home} [ JDK version ]" >&2
false
;;
esac
return $?
}