|
18 | 18 | # ---------------------------------------------------------------------------- |
19 | 19 |
|
20 | 20 | java_dist="" |
21 | | -java_dir="" |
| 21 | +default_java_dir="/usr/lib/jvm" |
| 22 | +java_dir="$default_java_dir" |
22 | 23 |
|
23 | | -function help() { |
| 24 | +function usage() { |
24 | 25 | echo "" |
25 | 26 | echo "Usage: " |
26 | | - echo "install-java.sh -f <java_dist> [-p] <java_dir>" |
| 27 | + echo "install-java.sh -f <java_dist> [-p <java_dir>]" |
27 | 28 | echo "" |
28 | | - echo "-f: The jdk tar.gz file" |
29 | | - echo "-p: Java installation directory" |
| 29 | + echo "-f: The jdk tar.gz file." |
| 30 | + echo "-p: Java installation directory. Default: $default_java_dir." |
| 31 | + echo "-h: Display this help and exit." |
30 | 32 | echo "" |
31 | 33 | } |
32 | 34 |
|
33 | | -confirm() { |
| 35 | +function confirm() { |
34 | 36 | # call with a prompt string or use a default |
35 | 37 | read -r -p "${1:-Are you sure?} [y/N] " response |
36 | 38 | case $response in |
@@ -50,93 +52,101 @@ if [ "$UID" -ne "0" ]; then |
50 | 52 | exit 9 |
51 | 53 | fi |
52 | 54 |
|
53 | | -while getopts "f:p:" opts; do |
| 55 | +while getopts "f:p:h" opts; do |
54 | 56 | case $opts in |
55 | 57 | f) |
56 | 58 | java_dist=${OPTARG} |
57 | 59 | ;; |
58 | 60 | p) |
59 | 61 | java_dir=${OPTARG} |
60 | 62 | ;; |
| 63 | + h) |
| 64 | + usage |
| 65 | + exit 0 |
| 66 | + ;; |
61 | 67 | \?) |
62 | | - help |
| 68 | + usage |
63 | 69 | exit 1 |
64 | 70 | ;; |
65 | 71 | esac |
66 | 72 | done |
67 | 73 |
|
68 | 74 | if [[ ! -f $java_dist ]]; then |
69 | | - echo "Please specify the Java distribution file (tar.gz)" |
70 | | - help |
| 75 | + echo "Please specify the Java distribution file." |
| 76 | + exit 1 |
| 77 | +fi |
| 78 | + |
| 79 | +# Validate Java Distribution |
| 80 | +java_dist_filename=$(basename $java_dist) |
| 81 | + |
| 82 | +if [[ ${java_dist_filename: -7} != ".tar.gz" ]]; then |
| 83 | + echo "Java distribution must be a valid tar.gz file." |
71 | 84 | exit 1 |
72 | 85 | fi |
73 | 86 |
|
74 | 87 | #Check whether unzip command exsits |
75 | 88 | if ! command -v unzip >/dev/null 2>&1; then |
76 | | - echo "Please install unzip (apt -y install unzip)" |
| 89 | + echo "Please install unzip (apt -y install unzip)." |
77 | 90 | exit 1 |
78 | 91 | fi |
79 | 92 |
|
80 | | -#If no directory was provided, we need to create the default one |
81 | | -if [[ -z $java_dir ]]; then |
82 | | - java_dir="/usr/lib/jvm" |
| 93 | +# Create the default directory if user has not specified any other path |
| 94 | +if [[ $java_dir == $default_java_dir ]]; then |
83 | 95 | mkdir -p $java_dir |
84 | 96 | fi |
85 | 97 |
|
86 | 98 | #Validate java directory |
87 | 99 | if [[ ! -d $java_dir ]]; then |
88 | | - echo "Please specify a valid Java installation directory" |
| 100 | + echo "Please specify a valid Java installation directory." |
89 | 101 | exit 1 |
90 | 102 | fi |
91 | 103 |
|
92 | | -# Validate Java Distribution |
93 | | -java_dist_filename=$(basename $java_dist) |
94 | | -echo "Installing $java_dist_filename" |
95 | | - |
96 | | -java_78_dist_file_regex="^jdk-([78])u([0-9]{1,3})-linux-(i586|x64)\.tar\.gz$" |
97 | | -java_9up_dist_file_regex="^(open)?jdk-([91][0-9]?\.?[0-9]*\.?[0-9]*)_linux-x(32|64)_bin\.tar\.gz$" |
| 104 | +echo "Installing: $java_dist_filename" |
98 | 105 |
|
99 | | -# JDK Directory with version (tar -tzf $java_dist | head -1 | cut -f1 -d"/") |
100 | | -jdk_dir="" |
101 | | -# JDK Major Version |
102 | | -jdk_major_version="" |
| 106 | +# Check Java executable |
| 107 | +java_exec="$(tar -tzf $java_dist | grep ^[^/]*/bin/java$ || echo "")" |
103 | 108 |
|
104 | | -if [[ $java_dist_filename =~ $java_78_dist_file_regex ]]; then |
105 | | - jdk_dir=$(echo $java_dist_filename | sed -nE "s/$java_78_dist_file_regex/jdk1.\1.0_\2/p") |
106 | | - jdk_major_version=$(echo $jdk_dir | sed -nE 's/jdk1\.([0-9]*).*/\1/p') |
107 | | -elif [[ $java_dist_filename =~ $java_9up_dist_file_regex ]]; then |
108 | | - jdk_dir=$(echo $java_dist_filename | sed -nE "s/$java_9up_dist_file_regex/jdk-\2/p") |
109 | | - jdk_major_version=$(echo $jdk_dir | sed -nE 's/jdk-([0-9]*).*/\1/p') |
110 | | -else |
111 | | - echo "Please specify a valid Java distribution" |
| 109 | +if [[ -z $java_exec ]]; then |
| 110 | + echo "Could not find \"java\" executable in the distribution. Please specify a valid Java distribution." |
112 | 111 | exit 1 |
113 | 112 | fi |
114 | 113 |
|
| 114 | +# JDK Directory with version |
| 115 | +jdk_dir="$(echo $java_exec | cut -f1 -d"/")" |
115 | 116 | extracted_dirname=$java_dir"/"$jdk_dir |
116 | 117 |
|
117 | 118 | # Extract Java Distribution |
118 | | - |
119 | 119 | if [[ ! -d $extracted_dirname ]]; then |
120 | 120 | echo "Extracting $java_dist to $java_dir" |
121 | 121 | tar -xof $java_dist -C $java_dir |
122 | 122 | echo "JDK is extracted to $extracted_dirname" |
123 | 123 | else |
124 | | - echo "WARN: JDK was not extracted to $extracted_dirname. The path already exists." |
| 124 | + echo "WARN: JDK was not extracted to $java_dir. There is an existing directory with name $jdk_dir." |
| 125 | + exit 1 |
125 | 126 | fi |
126 | 127 |
|
127 | | -if [[ ! -f $extracted_dirname"/bin/java" ]]; then |
| 128 | +if [[ ! -f "${extracted_dirname}/bin/java" ]]; then |
128 | 129 | echo "ERROR: The path $extracted_dirname is not a valid Java installation." |
129 | 130 | exit 1 |
130 | 131 | fi |
131 | 132 |
|
132 | | -# print "Java version" |
133 | | -echo "------------------------------------------------------------------------------" |
134 | | -${extracted_dirname}/bin/java -version |
135 | | -echo "------------------------------------------------------------------------------" |
| 133 | +# Oracle JDK: 7 to 8 |
| 134 | +java_78_dir_regex="^jdk1\.([0-9]*).*$" |
| 135 | +# Oracle JDK / OpenJDK / AdoptOpenJDK: 9 and upwards |
| 136 | +java_9up_dir_regex="^jdk-([0-9]*).*$" |
| 137 | + |
| 138 | +# JDK Major Version |
| 139 | +jdk_major_version="" |
| 140 | + |
| 141 | +if [[ $jdk_dir =~ $java_78_dir_regex ]]; then |
| 142 | + jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_78_dir_regex/\1/p") |
| 143 | +else |
| 144 | + jdk_major_version=$(echo $jdk_dir | sed -nE "s/$java_9up_dir_regex/\1/p") |
| 145 | +fi |
136 | 146 |
|
137 | 147 | # Install Demos |
138 | 148 |
|
139 | | -if [[ $java_dist_filename =~ $java_78_dist_file_regex ]]; then |
| 149 | +if [[ $jdk_dir =~ $java_78_dir_regex ]]; then |
140 | 150 | # Demos are only available for Java 7 and 8 |
141 | 151 | demos_dist=$(dirname $java_dist)"/"$(echo $java_dist_filename | sed 's/\.tar\.gz/-demos\0/') |
142 | 152 | fi |
|
154 | 164 |
|
155 | 165 | unlimited_jce_policy_dist="" |
156 | 166 |
|
157 | | -if [[ "$java_dist_filename" =~ ^jdk-7.* ]]; then |
| 167 | +if [[ $jdk_dir =~ ^jdk1\.7.* ]]; then |
158 | 168 | unlimited_jce_policy_dist="$(dirname $java_dist)/UnlimitedJCEPolicyJDK7.zip" |
159 | | -elif [[ "$java_dist_filename" =~ ^jdk-8.* ]]; then |
| 169 | +elif [[ $jdk_dir =~ ^jdk1\.8.* ]]; then |
160 | 170 | unlimited_jce_policy_dist="$(dirname $java_dist)/jce_policy-8.zip" |
161 | 171 | fi |
162 | 172 |
|
@@ -168,17 +178,14 @@ if [[ -f $unlimited_jce_policy_dist ]]; then |
168 | 178 | fi |
169 | 179 |
|
170 | 180 | # Run update-alternatives commands |
171 | | - |
172 | | -commands=("jar" "java" "javac" "javadoc" "javah" "javap" "javaws" "jcmd" "jconsole" "jarsigner" "jhat" "jinfo" "jmap" "jmc" "jps" "jstack" "jstat" "jstatd" "jvisualvm" "keytool" "policytool" "wsgen" "wsimport") |
173 | | - |
174 | 181 | if (confirm "Run update-alternatives commands?"); then |
175 | | - echo "Running update-alternatives --install and --config for ${commands[@]} mozilla-javaplugin.so" |
176 | | - |
177 | | - for i in "${commands[@]}"; do |
178 | | - command_path=$extracted_dirname/bin/$i |
179 | | - if [[ -f $command_path ]]; then |
180 | | - update-alternatives --install "/usr/bin/$i" "$i" "$command_path" 10000 |
181 | | - update-alternatives --set "$i" "$command_path" |
| 182 | + echo "Running update-alternatives..." |
| 183 | + declare -a commands=($(ls -1 ${extracted_dirname}/bin)) |
| 184 | + for command in "${commands[@]}"; do |
| 185 | + command_path=$extracted_dirname/bin/$command |
| 186 | + if [[ -x $command_path ]]; then |
| 187 | + update-alternatives --install "/usr/bin/$command" "$command" "$command_path" 10000 |
| 188 | + update-alternatives --set "$command" "$command_path" |
182 | 189 | fi |
183 | 190 | done |
184 | 191 |
|
|
192 | 199 | # Create system preferences directory |
193 | 200 | java_system_prefs_dir="/etc/.java/.systemPrefs" |
194 | 201 | if [[ ! -d $java_system_prefs_dir ]]; then |
195 | | - if (confirm "Create Java System Prefs Directory and change ownership to $SUDO_USER:$SUDO_USER?"); then |
| 202 | + if (confirm "Create Java System Prefs Directory ($java_system_prefs_dir) and change ownership to $SUDO_USER:$SUDO_USER?"); then |
196 | 203 | echo "Creating $java_system_prefs_dir" |
197 | 204 | mkdir -p $java_system_prefs_dir |
198 | 205 | chown -R $SUDO_USER:$SUDO_USER $java_system_prefs_dir |
199 | 206 | fi |
200 | 207 | fi |
201 | 208 |
|
202 | | -if (confirm "Do you want to set JAVA_HOME environment variable?"); then |
| 209 | +if (confirm "Do you want to set JAVA_HOME environment variable in $HOME/.bashrc?"); then |
203 | 210 | if grep -q "export JAVA_HOME=.*" $HOME/.bashrc; then |
204 | 211 | sed -i "s|export JAVA_HOME=.*|export JAVA_HOME=$extracted_dirname|" $HOME/.bashrc |
205 | 212 | else |
|