-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall-jdk
executable file
·224 lines (187 loc) · 5.52 KB
/
install-jdk
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
#!/usr/bin/env bash
# $1?: A JVM directory or a directory containing a compressed JDK. Defaults to
# the current directory
#
# Attempts to install a JDK to /usr/lib/jvm.
#
# This script will extract compressed JDK files and install them in
# /usr/lib/jvm. If the given directory is an already extracted JDK, it will be
# moved to /usr/lib/jvm. update-alternatives will be generated for the new JDK.
#
# If $1 is already in /usr/lib/jvm, only update-alternatives will be created.
if [[ $(id -u) -ne 0 ]]; then
echo "This script needs to be run as root."
exec sudo "PATH=$PATH" -k bash -$- "$0" "$@"
# Exec shouldn't return, but just in case.
exit $?
fi
############################
# BEGIN HELPER FUNCTIONS #
############################
# $1: jdk_dir
# $2: command name
_find_command() {
for dir in bin jre/bin jre/lib; do
file="$1/$dir/$2"
if [ -x "$file" ]; then
echo "$file"
return 0
fi
done
return 1
}
_is_jdk_dir() {
[ -d "$1" ] && [ -d "${1}/bin" ] &&\
_find_command "$1" java &>/dev/null &&\
_find_command "$1" javac &>/dev/null
}
# $1 must pass _is_jdk_dir
_get_version() {
local java
java=$(_find_command "$1" java)
$java -version |& sed -nre 's/(openjdk|java) version "(.+)".*/\2/p'
}
# $1 version number
# 1.8.0 -> 800000
# 1.8.0_56 -> 800056
# 1.8.0_121 -> 800121
# 1.8.0_1000 -> 800999
# 9 -> 900000
# 9.0.4 -> 900004
_get_priority() {
local major minor security
# Determine the major version.
if [[ $1 =~ ^1\..*$ ]]; then
# Old-style version string (1.8.0_123).
local IFS='.'
local split=( $1 )
unset IFS
major=${split[1]}
local IFS='_'
split=( ${split[2]} )
unset IFS
minor=${split[0]}
security=${split[1]}
else
# New-style JEP223 version string (9.0.4).
local IFS='.'
local split=( $1 )
unset IFS
major=${split[0]}
minor=${split[1]}
security=${split[2]}
fi
if [[ ${#minor} -gt 2 ]]; then
minor=99
else
minor=$(printf "%02d" "$minor")
fi
if [[ ${#security} -gt 3 ]]; then
security=999
else
security=$(printf "%03d" "$security")
fi
echo "${major}${minor}${security}"
}
# Report which line of the file caused the error.
_report_error() {
echo "Error occurred on line ${BASH_LINENO} of ${BASH_SOURCE}:" >&2
sed -ne "${BASH_LINENO}p" "${BASH_SOURCE}" >&2
}
# Clean up temp files.
_cleanup() {
if [ -n "$extract_to" ]; then
rm -rf "$extract_to"
fi
}
##########################
# END HELPER FUNCTIONS #
##########################
set -e -o pipefail
trap _report_error ERR
# Clean up temp files on exiting.
trap _cleanup EXIT
jdk_dir=$(realpath "${1:-$(pwd)}")
if ! _is_jdk_dir "$jdk_dir"; then
if [ -d "$jdk_dir" ]; then
echo "Looking for compressed JDK in $jdk_dir"
files=()
for file in "$jdk_dir"/*jdk*; do
[ -f "$file" ] && files+=( "$file" )
done
if [ ${#files[@]} -eq 0 ]; then
echo "Could not find a compressed JDK file."
exit 1
elif [ ${#files[@]} -eq 1 ]; then
file_to_install=${files[0]}
echo "Found: $file_to_install"
else
echo "Please select one of the following."
select file_to_install in "${files[@]}"; do
echo "Selected: $file_to_install"
break
done
fi
elif [ -f "$jdk_dir" ]; then
file_to_install="$jdk_dir"
else
echo "Unknown parameter $jdk_dir"
exit 1
fi
extract_to=$(mktemp -d)
case "$file_to_install" in
*.tar.gz)
pv -N "Extracting" "$file_to_install" | tar -xzf - -C "$extract_to"
;;
*)
echo "I don't know how to extract $file_to_install"
exit 1
;;
esac
children=( $(find "$extract_to" -mindepth 1 -maxdepth 1) )
if [[ ${#children[@]} -eq 1 ]]; then
jdk_dir=${children[0]}
else
jdk_dir=$extract_to
fi
if ! _is_jdk_dir "$jdk_dir"; then
echo "$file_to_install was not a JDK."
exit 1
fi
fi
if [[ $jdk_dir = /usr/lib/jvm/* ]]; then
version=$(_get_version "$jdk_dir")
else
version=$(_get_version "$jdk_dir")
jdk_name="jdk${version}"
new_location=/usr/lib/jvm/$jdk_name
if [ -e "$new_location" ]; then
echo "$new_location already exists!"
exit 1
fi
parent=$(dirname "$new_location")
mkdir -p "$parent"
mv "$jdk_dir" "$new_location"
jdk_dir=$new_location
chown -R root:root "$jdk_dir"
fi
slave_commands=( appletviewer extcheck idlj jar jarsigner java javac javadoc \
javah javap jcmd jconsole jdb jexec jhat jinfo jps jrunscript jsadebugd \
jshell jstack jstat jstatd keytool native2ascii orbd pack200 policytool \
rmic rmid rmiregistry schemagen serialver servertool tnameserv unpack200 wsgen \
wsimport xjc )
priority=$(_get_priority "$version")
command="--install /usr/lib/jvm/default-java default-java"
command+=" \"$jdk_dir\" $priority"
for cmd in "${slave_commands[@]}"; do
echo "Updating $cmd"
path=$(_find_command "$jdk_dir" "$cmd" || echo)
if [ -n "$path" ]; then
# If it was previously set up as a master link, remove all entries.
update-alternatives --remove-all $cmd &>/dev/null || true
command+=" --slave /usr/bin/$cmd $cmd \"$path\""
else
echo "Could not find $cmd"
fi
done
eval update-alternatives $command