-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharchlinux-java-run.sh
executable file
·392 lines (344 loc) · 10.6 KB
/
archlinux-java-run.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/bin/bash
#
# (c) 2017, 2018, 2019, 2020, 2023, 2024 Michael Lass
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# This script uses `exec` on purpose to launch a suitable JRE before the end of
# the script.
# shellcheck disable=SC2093
VERSION=11
JAVADIR=###JAVADIR###
JAVAFX_MODULES=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web
function print_usage {
cat << EOF
USAGE:
archlinux-java-run [-a|--min MIN] [-b|--max MAX] [-p|--package PKG]
[-f|--feature FEATURE] [-h|--help] [-v|--verbose]
[-d|--dry-run] [-j|--java-home]
-- JAVA_ARGS
EOF
}
function print_help {
cat << EOF
archlinux-java-run, Version v$VERSION
archlinux-java-run is a helper script used to launch Java applications
that have specific demands on version or provider of the used JVM.
Options can be arbitrarily combined and archlinux-java-run will try to
find a suitable version. If the user's default JVM is eligible, it will
be used. Otherwise, if multiple eligible versions are installed, the
newest Java generation is used. If multiple packages are available for
this version, the one corresponding to the user's default JVM is used.
By default, archlinux-java-run will execute a suitable version of java
with the given JAVA_ARGS. When run with -j|--java-home, it just prints
the location of a suitable java installation so that custom commands
can be run.
EOF
print_usage
cat << EOF
AVAILABLE FEATURES:
javafx: Test if JVM provides support for JavaFX. For JVM versions above 8
this will modify the module path and the list of loaded modules to
make JavaFX available. CAUTION: Software developed for Java >8 using
JavaFX typically provides and loads its own copy of OpenJFX. The
feature should not be requested in this case.
jdk: Test if the installation is a full JDK and not just a JRE, i.e., it
includes javac.
EXAMPLES:
archlinux-java-run --max 8 -- -jar /path/to/application.jar
(launches java in version 8 or below)
archlinux-java-run --package 'jre/jre|jdk' -- -jar /path/to/application.jar
(launches Oracle's java from one of the jre or jdk AUR packages)
archlinux-java-run --feature 'javafx' -- -jar /path/to/application.jar
(launches a JVM that supports JavaFX)
JAVA_HOME=\$(archlinux-java-run --min 11 --feature jdk --java-home) \\
&& "\$JAVA_HOME"/bin/javac ...
(launches javac from a JDK in version 11 or newer)
EOF
}
function echo_stderr {
echo "$1" 1>&2
}
function is_in {
[[ "$1" =~ (^| )"$2"($| ) ]]
}
function normalize_name {
re_default="^java-([0-9]+)-(.+)\$"
re_short="^(.+)-([0-9]+)\$"
re_split="^([-A-Za-z]*[A-Za-z]+)-?([0-9]+)-(.+)\$"
if [[ $1 =~ $re_default ]]; then
echo -n "$1"
elif [[ $1 =~ $re_short ]]; then
echo -n "java-${BASH_REMATCH[2]}-${BASH_REMATCH[1]}"
elif [[ $1 =~ $re_split ]]; then
echo -n "java-${BASH_REMATCH[2]}-${BASH_REMATCH[1]}-${BASH_REMATCH[3]}"
else
echo_stderr "ERROR: Could not parse JRE name $1"
fi
}
available=$(archlinux-java status | tail -n+2 | cut -d' ' -f3 | sort -rV -t- -k2 | xargs)
default=$(archlinux-java get)
if [ -z "$default" ]; then
echo_stderr "Your Java installation is not set up correctly. Try archlinux-java fix."
exit 1
fi
# Default boundaries for Java versions
min=-1
max=-1
for ver in $available; do
major=$(normalize_name "$ver" | cut -d- -f2)
if [ "$major" -gt "$max" ]; then
max="$major"
fi
if [ "$major" -lt "$min" ] || [ "$min" -eq -1 ]; then
min="$major"
fi
done
function generate_candiates {
local list
local pref_package
pref_package=$(cut -d- -f3- <<< "$(normalize_name "$default")")
local exp
exp="($(seq "$min" "$max"|paste -sd'|'))"
if [ -n "$package" ]; then
exp="^java-${exp}-(${package})\$"
else
exp="^java-${exp}-.*\$"
fi
# we want to try the user's default JRE first
if [[ $(normalize_name "$default") =~ $exp ]]; then
list="$default "
fi
local subexp
for i in $(seq "$max" -1 "$min"); do
# try JRE that matches the user's default package
subexp="^java-${i}-${pref_package}\$"
for ver in $available; do
norm_ver=$(normalize_name "$ver")
if [[ $norm_ver =~ $exp && $norm_ver =~ $subexp ]]; then
if ! is_in "$list" "$ver"; then
list="$list$ver "
fi
fi
done
# try openjdk since it is Arch's default
subexp="^java-${i}-openjdk\$"
for ver in $available; do
norm_ver=$(normalize_name "$ver")
if [[ $norm_ver =~ $exp && $norm_ver =~ $subexp ]]; then
if ! is_in "$list" "$ver"; then
list="$list$ver "
fi
fi
done
# try everything else
for ver in $available; do
norm_ver=$(normalize_name "$ver")
if [[ $norm_ver =~ $exp ]]; then
if ! is_in "$list" "$ver"; then
list="$list$ver "
fi
fi
done
done
echo "$list" | xargs
}
function test_javafx_support() {
if [ "$major" -lt 9 ]; then
testcmd="/usr/lib/jvm/${ver}/bin/java -jar ${JAVADIR}/archlinux-java-run/TestJavaFX.jar"
else
mpath=$(eval echo "/usr/lib/jvm/{${ver},java-${major}-openjfx}/lib/{javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web,javafx-swt}.jar" | tr ' ' :)
testcmd="/usr/lib/jvm/${ver}/bin/java --module-path ${mpath} --add-modules ALL-MODULE-PATH -jar ${JAVADIR}/archlinux-java-run/TestJavaFX.jar"
fi
if [ "$verbose" -eq 1 ]; then
echo_stderr "Testing JavaFX support: $testcmd"
fi
$testcmd
}
function test_jdk_support() {
test -x /usr/lib/jvm/"$ver"/bin/javac
}
function extend_java_args() {
local updated=0
for i in "${!java_args[@]}"; do
case "${java_args[i]}" in
--"$1"=*)
java_args[i]="${java_args[i]}$3$2"
updated=1
;;
--"$1")
java_args[i+1]="${java_args[i+1]}$3$2"
updated=1
;;
esac
done
if [ $updated -eq 0 ]; then
java_args=("--$1=$2" "${java_args[@]}")
fi
}
function quote_args() {
for arg in "${java_args[@]}"; do
if [[ $arg =~ " " ]]; then
quoted_java_args+=("${arg@Q}")
else
quoted_java_args+=("${arg}")
fi
done
}
args=( )
for arg; do
case "$arg" in
--min) args+=( -a ) ;;
--max) args+=( -b ) ;;
--help) args+=( -h ) ;;
--package) args+=( -p ) ;;
--feature) args+=( -f ) ;;
--verbose) args+=( -v ) ;;
--dry-run) args+=( -d ) ;;
--java-home) args+=( -j ) ;;
*) args+=( "$arg" ) ;;
esac
done
set -- "${args[@]}"
features=( )
java_args=( )
quoted_java_args=( )
verbose=0
dryrun=0
javahome=0
args_parsed=0
while :; do
if [ $args_parsed -eq 0 ]; then
case "$1" in
-a) case "$2" in
''|*[!0-9]*) echo_stderr "-a|--min expects an integer argument"
exit 1
;;
*) min=$2
shift
;;
esac
;;
-b) case "$2" in
''|*[!0-9]*) echo_stderr "-b|--max expects an integer argument"
exit 1
;;
*) max=$2
shift
;;
esac
;;
-h) print_help
exit 0
;;
-p) case "$2" in
''|-*|*' '*) echo_stderr "-p|--package expects exactly one argument"
exit 1
;;
*) package=$2
shift
;;
esac
;;
-f) case "$2" in
''|-*|*' '*) echo_stderr "-f|--feature expects exactly one argument"
exit 1
;;
*) features+=( "$2" )
shift
;;
esac
;;
-v) verbose=1
;;
-d) dryrun=1
;;
-j) javahome=1
;;
--) args_parsed=1
;;
'') break
;;
*) echo_stderr "Unknown argument: $1"
print_usage 1>&2
exit 1
;;
esac
else
[ "$1" == '' ] && break
java_args+=("$1")
fi
shift
done
candidates=$(generate_candiates)
for ver in $candidates; do
major=$(normalize_name "$ver" | cut -d- -f2)
# Test for each of the required features
for ft in "${features[@]}"; do
case "$ft" in
jdk)
if ! test_jdk_support; then
continue 2
fi
;;
javafx)
if ! test_javafx_support; then
continue 2
fi
;;
*)
echo_stderr "Ignoring request for unknown feature $ft"
;;
esac
done
if [ $javahome -eq 1 ]; then
echo "/usr/lib/jvm/${ver}"
exit 0
fi
for ft in "${features[@]}"; do
case "$ft" in
javafx)
if [ "$major" -gt 8 ]; then
echo_stderr "Modifying java arguments to support system installation of JavaFX"
additional_mpath=$(eval echo "/usr/lib/jvm/{${ver},java-${major}-openjfx}/lib/{javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web,javafx-swt}.jar" | tr ' ' :)
extend_java_args module-path "$additional_mpath" ':'
extend_java_args add-modules "$JAVAFX_MODULES" ','
fi
;;
esac
done
quote_args
if [ $dryrun -eq 1 ]; then
echo "DRY-RUN - Generated command: /usr/lib/jvm/${ver}/bin/java ${quoted_java_args[*]}"
exit 0
fi
if [ $verbose -eq 1 ]; then
echo_stderr "Executing command: /usr/lib/jvm/${ver}/bin/java ${quoted_java_args[*]}"
fi
exec /usr/lib/jvm/"$ver"/bin/java "${java_args[@]}"
done
echo_stderr "No suitable JVM found."
echo_stderr "Available: $available"
echo_stderr "Default: $default"
echo_stderr "Min. required: $min"
echo_stderr "Max. required: $max"
echo_stderr "Package required: $package"
echo_stderr "Candidates: $candidates"
echo_stderr "Features required: ${features[*]}"
exit 1