aboutsummaryrefslogtreecommitdiffstats
path: root/java/javavmwrapper/src/javavmwrapper.sh
blob: b1c555b3f4316cde34bc7de5bf838f19a9be90cf (plain) (blame)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/bin/sh
#
# javawrapper.sh
#
# Allow the installation of several Java Virtual Machines on the system.
# They can then be selected from based on environment variables and the
# configuration file.
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
# Maxim Sobolev <sobomax@FreeBSD.org> wrote this file.  As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some
# day, and you think this stuff is worth it, you can buy me a beer in return.
#
# Maxim Sobolev
# ----------------------------------------------------------------------------
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
# Greg Lewis <glewis@FreeBSD.org> substantially rewrote this file.  As long
# as you retain this notice you can do whatever you want with this stuff. If
# we meet some day, and you think this stuff is worth it, you can buy me a
# beer in return.
#
# Greg Lewis
# ----------------------------------------------------------------------------
#
# $FreeBSD$
#
# MAINTAINER=java@FreeBSD.org

_JAVAVM_SAVE_PATH=${PATH}
export PATH=/bin:/sbin:/usr/bin:/usr/sbin

_JAVAVM_PREFIX="%%PREFIX%%"
_JAVAVM_CONF="${_JAVAVM_PREFIX}/etc/javavms"
_JAVAVM_OPTS_CONF="${_JAVAVM_PREFIX}/etc/javavm_opts.conf"
_JAVAVM_PROG=`basename "${0}"`
_JAVAVM_MAKE=/usr/bin/make

#
# Try to run a Java command.
#
tryJavaCommand () {
    # Check for the command being executable and exec it if so.
    if [ -x "${1}" ]; then
        if [ ! -z "${_JAVAVM_SAVE_PATH}" ]; then
            export PATH=${_JAVAVM_SAVE_PATH}
        fi
        exec "${@}"
    fi

    echo "${_JAVAVM_PROG}: warning: couldn't run specified Java command - \"${1}\"" >&2
}

#
# Set java command options, if applicable
# 1 - Name of the java programme to be executed.
# 2 - Base directory of JAVA_HOME for the java programme to be executed.
#
setJavaOptions () {
    local OPTS_PROG=`echo ${1} | sed -e s/\\\\./_/g -e s/-/_/g`
    local OPTS_JAVA_HOME=`echo ${2} | sed -e s/\\\\./_/g -e s/-/_/g`
    local JAVA_HOME_PROG_OPTS="`eval echo \$\{JAVAVM_OPTS_${OPTS_JAVA_HOME}_${OPTS_PROG}\} 2>/dev/null`"
    local JAVA_HOME_OPTS="`eval echo \$\{JAVAVM_OPTS_${OPTS_JAVA_HOME}\} 2>/dev/null`"
    local PROG_OPTS="`eval echo \$\{JAVAVM_OPTS_${OPTS_PROG}\} 2>/dev/null`"

    # Possible environment variables are stackable
    if [ ! -z "${JAVA_HOME_PROG_OPTS}" ]; then
        _JAVAVM_OPTS="${_JAVAVM_OPTS} ${JAVA_HOME_PROG_OPTS}"
    fi
    if [ ! -z "${JAVA_HOME_OPTS}" ]; then
        _JAVAVM_OPTS="${_JAVAVM_OPTS} ${JAVA_HOME_OPTS}"
    fi
    if [ ! -z "${PROG_OPTS}" ]; then
        _JAVAVM_OPTS="${_JAVAVM_OPTS} ${PROG_OPTS}"
    fi
    if [ ! -z "${JAVAVM_OPTS}" ]; then
        _JAVAVM_OPTS="${_JAVAVM_OPTS} ${JAVAVM_OPTS}"
    fi
}

#
# Create symbolic links for all of a Java VMs executables.
#
createJavaLinks () {
    for exe in "${1}"/bin/* "${1}"/jre/bin/*; do
        if [ -x "${exe}" -a \
             ! -d "${exe}" -a \
             "`basename "${exe}"`" = "`basename "${exe}" .so`" -a \
             ! -e "${_JAVAVM_PREFIX}/bin/`basename "${exe}"`" -a \
             -w "${_JAVAVM_PREFIX}/bin" ]; then
            ln -s "${_JAVAVM_PREFIX}/bin/javavm" \
                "${_JAVAVM_PREFIX}/bin/`basename "${exe}"`" 2>/dev/null
        fi
    done
}

#
# Sort the configuration file
#
sortConfiguration () {
    local IFS=:

    # Ensure the configuration file exists
    if [ ! -f "${_JAVAVM_CONF}" ]; then
        return
    fi

    # Ensure the configuration file has the correct permissions
    if [ ! -w "${_JAVAVM_CONF}" -o ! -r "${_JAVAVM_CONF}" ]; then
        echo "${_JAVAVM_PROG}: error: can't read/write ${_JAVAVM_CONF} configuration file!" >&2
        return
    fi

    cat "${_JAVAVM_CONF}" | \
    (
    export JAVAVMS
    while read JAVAVM; do
        VM=`echo "${JAVAVM}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
        # Check that the VM exists and is "sane"
        if [ ! -e "${VM}" ]; then
            continue
        fi
        if [ -d "${VM}" ]; then
            continue
        fi
        if [ ! -x "${VM}" ]; then
            continue
        fi
        if [ `basename "${VM}"` != "java" ]; then
            continue
        fi
        if [ "`realpath "${VM}" 2>/dev/null `" = "${_JAVAVM_PREFIX}/bin/javavm" ]; then
            continue
        fi
        # Skip duplicate VMs
        if [ ! -z "${JAVAVMS}" ]; then
            for _JAVAVM in ${JAVAVMS}; do
                if [ -z "${_JAVAVM}" ]; then
                    continue
                fi
                _VM=`echo "${_JAVAVM}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
                if [ "x${VM}" = "x${_VM}" ]; then
                    continue 2
                fi
            done
        fi
        VM=`dirname "${VM}"`
        VM=`dirname "${VM}"`
        VM=`basename "${VM}"`
        _JAVAVMS=:
        for _JAVAVM in ${JAVAVMS}; do
            if [ -z "${_JAVAVM}" ]; then
                continue
            fi
            if [ -z "${JAVAVM}" ]; then
                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                continue
            fi
            _VM=`echo "${_JAVAVM}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
            _VM=`dirname "${_VM}"`
            _VM=`dirname "${_VM}"`
            _VM=`basename "${_VM}"`
            VERSION=`echo ${VM} | sed -e 's|[^0-9]*||' 2>/dev/null`
            _VERSION=`echo ${_VM} | sed -e 's|[^0-9]*||' 2>/dev/null`
            if [ "${VERSION}" \> "${_VERSION}" ]; then
                _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                JAVAVM=
                continue
            elif [ "${VERSION}" \< "${_VERSION}" ]; then
                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                continue
            else
                case "${VM}" in
                    diablo-jdk*)
                        _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                        JAVAVM=
                        continue
                        ;;
                    diablo-jre*|jdk*)
                        case "${_VM}" in
                            diablo*)
                                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                                continue
                                ;;
                            *)
                                _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                                JAVAVM=
                                continue
                                ;;
                        esac
                        ;;
                    jre*|linux-sun-jdk*)
                        case "${_VM}" in
                            diablo*|j*)
                                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                                continue
                                ;;
                            *)
                                _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                                JAVAVM=
                                continue
                                ;;
                        esac
                        ;;
                    linux-sun-jre*|linux-blackdown-jdk*)
                        case "${_VM}" in
                            diablo*|j*|linux-sun*)
                                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                                continue
                                ;;
                            *)
                                _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                                JAVAVM=
                                continue
                                ;;
                        esac
                        ;;
                    linux-blackdown-jre*|linux-ibm-jdk*)
                        case "${_VM}" in
                            diablo*|j*|linux-sun*|linux-blackdown*)
                                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
                                continue
                                ;;
                            *)
                                _JAVAVMS="${_JAVAVMS}:${JAVAVM}:${_JAVAVM}"
                                JAVAVM=
                                continue
                                ;;
                        esac
                        ;;
                esac
                _JAVAVMS="${_JAVAVMS}:${_JAVAVM}"
            fi
        done
        JAVAVMS="${_JAVAVMS}"
        if [ ! -z "${JAVAVM}" ]; then
            JAVAVMS="${JAVAVMS}:${JAVAVM}"
        fi
    done;
    if [ ! -z "${JAVAVMS}" ]; then
        rm "${_JAVAVM_CONF}"
        for JAVAVM in ${JAVAVMS}; do
            if [ ! -z "${JAVAVM}" ]; then
                echo "${JAVAVM}" >> "${_JAVAVM_CONF}"
            fi
        done
    fi
    )
}

#
# Check all of the VMs in the configuration file
#
checkVMs () {
    # Ensure the configuration file exists
    if [ ! -f "${_JAVAVM_CONF}" ]; then
        exit 0
    fi

    # Ensure the configuration file has the correct permissions
    if [ ! -w "${_JAVAVM_CONF}" -o ! -r "${_JAVAVM_CONF}" ]; then
        echo "${_JAVAVM_PROG}: error: can't read/write ${_JAVAVM_CONF} configuration file!" 1>&2
        exit 1
    fi

    # Sort the configuration.  This will also remove duplicates and
    # non-existent VMs
    sortConfiguration

    # Ensure links are created for every executable for a VM.
    cat "${_JAVAVM_CONF}" | \
    (
    while read JAVAVM; do
        VM=`echo "${JAVAVM}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
        # Create symbolic links as appropriate if they don't exist.
        JAVA_HOME=`dirname "${VM}"`
        JAVA_HOME=`dirname "${JAVA_HOME}"`
        createJavaLinks "${JAVA_HOME}"
    done;
    )

    exit 0
}

#
# Register a new Java VM.
#
registerVM () {
    # Check the java command given to us.
    if [ -z "${1}" ]; then
       echo "Usage: ${_JAVAVM_PROG} path"
       exit 1
    fi

    # Create the configuration file if it doesn't exist
    if [ ! -e "${_JAVAVM_CONF}" ]; then
       touch "${_JAVAVM_CONF}"
    fi

    # Ensure the configuration file exists and has the correct permissions
    if [ ! -f "${_JAVAVM_CONF}" -o ! -w "${_JAVAVM_CONF}" -o ! -r "${_JAVAVM_CONF}" ]; then
        echo "${_JAVAVM_PROG}: error: can't read/write ${_JAVAVM_CONF} configuration file!" 1>&2
        exit 1
    fi

    # Check that the given VM can be found in the configuration file
    VM=`echo "${1}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
    REGISTERED=
    if [ ! -z "`grep "${VM}" "${_JAVAVM_CONF}"`" ]; then
        echo "${_JAVAVM_PROG}: warning: JavaVM \"${VM}\" is already registered" 1>&2
        REGISTERED="yes"
    fi

    # Check that the VM exists and is "sane"
    if [ ! -e "${VM}" ]; then
        echo "${_JAVAVM_PROG}: error: JavaVM \"${VM}\" does not exist" 1>&2
        exit 1
    fi
    if [ -d "${VM}" ]; then
        echo "${_JAVAVM_PROG}: error: JavaVM \"${VM}\" is a directory" 1>&2
        exit 1
    fi
    if [ ! -x "${VM}" ]; then
        echo "${_JAVAVM_PROG}: error: JavaVM \"${VM}\" is not executable" 1>&2
        exit 1
    fi
    if [ `basename "${VM}"` != "java" ]; then
        echo "${_JAVAVM_PROG}: error: JavaVM \"${VM}\" is not valid" 1>&2
        exit 1
    fi
    if [ "`realpath "${VM}" 2>/dev/null `" = "${_JAVAVM_PREFIX}/bin/javavm" ]; then
        echo "${_JAVAVM_PROG}: error: JavaVM \"${VM}\" is javavm!" 1>&2
        exit 1
    fi

    # Add the VM to the configuration file
    if [ "${REGISTERED}" != "yes" ]; then
        echo "${1}" >> "${_JAVAVM_CONF}"
    fi

    # Create symbolic links as appropriate if they don't exist.
    JAVA_HOME=`dirname "${VM}"`
    JAVA_HOME=`dirname "${JAVA_HOME}"`
    createJavaLinks "${JAVA_HOME}"

    # Sort the VMs
    sortConfiguration

    exit 0
}

#
# Unregister a Java VM.
#
unregisterVM () {
    # Check usage
    if [ -z "${1}" ]; then
       echo "Usage: ${_JAVAVM_PROG} path"
       exit 1
    fi

    # Check for the configuration file
    if [ ! -e "${_JAVAVM_CONF}" ]; then
       echo "${_JAVAVM_PROG}: error: can't find ${_JAVAVM_CONF} configuration file!" >&2
       exit 1
    fi

    # Ensure the configuration file has the correct permissions
    if [ ! -w "${_JAVAVM_CONF}" -o ! -r "${_JAVAVM_CONF}" ]; then
        echo "${_JAVAVM_PROG}: error: can't read/write ${_JAVAVM_CONF} configuration file!" >&2
        exit 1
    fi

    # Check that the given VM can be found in the configuration file
    if [ -z "`grep "${1}" "${_JAVAVM_CONF}"`" ]; then
        echo "${_JAVAVM_PROG}: error: \"${1}\" JavaVM is not currently registered"
        exit 1
    fi

    # Remove unneeded symlinks
    VMS=`sed -E 's|[[:space:]]*#.*||' < "${_JAVAVM_CONF}" | uniq 2>/dev/null`
    VM=`grep "${1}" "${_JAVAVM_CONF}" | sed -E 's|[[:space:]]*#.*||' 2>/dev/null`
    JAVA_HOME=`dirname "${VM}"`
    JAVA_HOME=`dirname "${JAVA_HOME}"`
    for exe in "${JAVA_HOME}"/bin/* "${JAVA_HOME}"/jre/bin/*; do
        exe=`basename "${exe}"`
        if [ -L "${_JAVAVM_PREFIX}/bin/${exe}" -a \
             "`realpath "${_JAVAVM_PREFIX}/bin/${exe}" 2>/dev/null `" = \
             "${_JAVAVM_PREFIX}/bin/javavm" ]; then
            for JAVAVM in ${VMS}; do
                if [ "${JAVAVM}" != "${VM}" ]; then
                    JAVAVM=`dirname "${JAVAVM}"`
                    JAVAVM=`dirname "${JAVAVM}"`
                    if [ -x "${JAVAVM}/bin/${exe}" -o \
                         -x "${JAVAVM}/jre/bin/${exe}" ]; then
                        continue 2
                    fi
                fi
            done

            rm "${_JAVAVM_PREFIX}/bin/${exe}"
        fi
    done

    # Remove the VM from the configuration file
    ed "${_JAVAVM_CONF}" >/dev/null <<EOF
g|${1}|d
w
q
EOF

    # Remove configuration file if its size reached 0
    if [ ! -s "${_JAVAVM_CONF}" ]; then
        rm "${_JAVAVM_CONF}"
    fi

    exit 0
}

# Check for an alias and call the appropriate function.
case "${_JAVAVM_PROG}" in
    registervm )
        registerVM "${1}"
        ;;
    unregistervm )
        unregisterVM "${1}"
        ;;
    checkvms )
        checkVMs
        ;;
esac

# Main ()

# Backwards compatibility
if [ "${_JAVAVM_PROG}" = "javavm" ]; then
    echo "${_JAVAVM_PROG}: warning: The use of 'javavm' as a synonym for 'java' is deprecated" 1>&2
    _JAVAVM_PROG=java
fi

# Initialise options
if [ -r "${_JAVAVM_OPTS_CONF}" ]; then
    . "${_JAVAVM_OPTS_CONF}"
fi
_JAVAVM_OPTS=

# Ignore JAVA_HOME if it's set to %%PREFIX%%
if [ "`realpath "${JAVA_HOME}"`" != "`realpath "${_JAVAVM_PREFIX}"`" ]; then
    # Otherwise use JAVA_HOME if it's set
    if [ ! -z "${JAVA_HOME}" -a -x "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    elif [ ! -z "${JAVA_HOME}" -a -x "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    fi
fi

unset JAVA_HOME

# Determine location of bsd.port.mk if it exists
_JAVAVM_PORTSDIR=
if [ -r /usr/share/mk/bsd.port.mk ]; then
    _JAVAVM_PORTSDIR=`"${_JAVAVM_MAKE}" -f /usr/share/mk/bsd.port.mk -V PORTSDIR 2>/dev/null`
fi

_JAVAVM_BSD_PORT_MK=
if [ ! -z "${_JAVAVM_PORTSDIR}" -a -r "${_JAVAVM_PORTSDIR}/Mk/bsd.port.mk" ]; then
    _JAVAVM_BSD_PORT_MK="${_JAVAVM_PORTSDIR}/Mk/bsd.port.mk"
fi

# If bsd.port.mk was found, use that to determine the VM to use.
if [ ! -z "${_JAVAVM_BSD_PORT_MK}" ]; then
    JAVA_HOME=`"${_JAVAVM_MAKE}" -f "${_JAVAVM_BSD_PORT_MK}" -V JAVA_HOME USE_JAVA=yes 2>/dev/null`
    if [ ! -z "${JAVA_HOME}" -a \
         -x "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    elif [ ! -z "${JAVA_HOME}" -a \
           -x "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    fi
fi

# Then try to make sure that ${_JAVAVM_CONF} exists
if [ ! -e "${_JAVAVM_CONF}" ]; then
    echo "${_JAVAVM_PROG}: error: can't find ${_JAVAVM_CONF} configuration file" >&2
    exit 1
fi

# Allow comments in the ${_JAVAVM_CONF}
_JAVAVM_VMS=`sed -E 's|[[:space:]]*#.*||' < "${_JAVAVM_CONF}" | uniq 2>/dev/null`

# Fix up JAVA_VERSION
if [ ! -z "${JAVA_VERSION}" ]; then
    _JAVAVM_VERSION=
    for version in ${JAVA_VERSION}; do
        case "${version}" in
            1.1+)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} 1.1 1.2 1.3 1.4 1.5"
                ;;
            1.2+)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} 1.2 1.3 1.4 1.5"
                ;;
            1.3+)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} 1.3 1.4 1.5"
                ;;
            1.4+)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} 1.4 1.5"
                ;;
            1.5+)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} 1.5"
                ;;
            *)
                _JAVAVM_VERSION="${_JAVAVM_VERSION} ${version}"
                ;;
        esac
    done
    JAVA_VERSION=`echo "${_JAVAVM_VERSION}" | sort | uniq`
fi

# Finally try to run one of the ${_JAVAVM_VMS}
for _JAVAVM_JAVAVM in ${_JAVAVM_VMS}; do
    JAVA_HOME=`dirname "${_JAVAVM_JAVAVM}"`
    JAVA_HOME=`dirname "${JAVA_HOME}"`
    _JAVAVM_VM=`basename "${JAVA_HOME}"`
    # Respect JAVA_VERSION
    if [ ! -z "${JAVA_VERSION}" ]; then
        _JAVAVM_VERSION=`echo ${_JAVAVM_VM} | \
            sed -e 's|[^0-9]*\([0-9]\)\.\([0-9]\)\.[0-9]|\1.\2|'`
        for _JAVAVM_REQUESTED_VERSION in ${JAVA_VERSION}; do
            if [ "${_JAVAVM_VERSION}" = "${_JAVAVM_REQUESTED_VERSION}" ]; then
                _JAVAVM_VERSION=
                break
            fi
        done
        if [ ! -z "${_JAVAVM_VERSION}" ]; then
            continue
        fi
    fi
    # Respect JAVA_OS
    if [ ! -z "${JAVA_OS}" ]; then
        _JAVAVM_OS=
        case "${_JAVAVM_VM}" in
            diablo*|j*)
                _JAVAVM_OS=native
                ;;
            linux*)
                _JAVAVM_OS=linux
                ;;
        esac
        for _JAVAVM_REQUESTED_OS in ${JAVA_OS}; do
            if [ "${_JAVAVM_OS}" = "${_JAVAVM_REQUESTED_OS}" ]; then
                _JAVAVM_OS=
                break
            fi
        done
        if [ ! -z "${_JAVAVM_OS}" ]; then
            continue
        fi
    fi
    # Respect JAVA_VENDOR
    if [ ! -z "${JAVA_VENDOR}" ]; then
        _JAVAVM_VENDOR=
        case "${_JAVAVM_VM}" in
            diablo*)
                _JAVAVM_VENDOR=bsdjava
                ;;
            j*)
                _JAVAVM_VENDOR=freebsd
                ;;
            linux-blackdown*)
                _JAVAVM_VENDOR=blackdown
                ;;
            linux-ibm*)
                _JAVAVM_VENDOR=ibm
                ;;
            linux-sun*)
                _JAVAVM_VENDOR=sun
                ;;
        esac
        for _JAVAVM_REQUESTED_VENDOR in ${JAVA_VENDOR}; do
            if [ "${_JAVAVM_VENDOR}" = "${_JAVAVM_REQUESTED_VENDOR}" ]; then
                _JAVAVM_VENDOR=
                break
            fi
        done
        if [ ! -z "${_JAVAVM_VENDOR}" ]; then
            continue
        fi
    fi
    # Check if the command exists and try to run it.
    if [ ! -z "${JAVA_HOME}" -a \
         -x "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    elif [ ! -z "${JAVA_HOME}" -a \
           -x "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ]; then
        setJavaOptions "${_JAVAVM_PROG}" "`basename ${JAVA_HOME}`"
        export JAVA_HOME
        tryJavaCommand "${JAVA_HOME}/jre/bin/${_JAVAVM_PROG}" ${_JAVAVM_OPTS} "${@}"
    fi
done

echo "${_JAVAVM_PROG}: error: no suitable JavaVMs found" >&2
exit 1