1#!/bin/sh
2
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18# -----------------------------------------------------------------------------
19#  Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings
20#  are valid and consistent with the selected start-up options and set up the
21#  endorsed directory.
22# -----------------------------------------------------------------------------
23
24# Make sure prerequisite environment variables are set
25if [ -z "$JAVA_HOME" ] && [ -z "$JRE_HOME" ]; then
26  if $darwin; then
27    # Bugzilla 54390
28    if [ -x '/usr/libexec/java_home' ] ; then
29      export JAVA_HOME=`/usr/libexec/java_home`
30    # Bugzilla 37284 (reviewed).
31    elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
32      export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
33    fi
34  else
35    JAVA_PATH=`which java 2>/dev/null`
36    if [ "x$JAVA_PATH" != "x" ]; then
37      JAVA_PATH=`dirname "$JAVA_PATH" 2>/dev/null`
38      JRE_HOME=`dirname "$JAVA_PATH" 2>/dev/null`
39    fi
40    if [ "x$JRE_HOME" = "x" ]; then
41      # XXX: Should we try other locations?
42      if [ -x /usr/bin/java ]; then
43        JRE_HOME=/usr
44      fi
45    fi
46  fi
47  if [ -z "$JAVA_HOME" ] && [ -z "$JRE_HOME" ]; then
48    echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
49    echo "At least one of these environment variable is needed to run this program"
50    exit 1
51  fi
52fi
53if [ -z "$JAVA_HOME" ] && [ "$1" = "debug" ]; then
54  echo "JAVA_HOME should point to a JDK in order to run in debug mode."
55  exit 1
56fi
57if [ -z "$JRE_HOME" ]; then
58  JRE_HOME="$JAVA_HOME"
59fi
60
61# If we're running under jdb, we need a full jdk.
62if [ "$1" = "debug" ] ; then
63  if [ "$os400" = "true" ]; then
64    if [ ! -x "$JAVA_HOME"/bin/java ] || [ ! -x "$JAVA_HOME"/bin/javac ]; then
65      echo "The JAVA_HOME environment variable is not defined correctly"
66      echo "This environment variable is needed to run this program"
67      echo "NB: JAVA_HOME should point to a JDK not a JRE"
68      exit 1
69    fi
70  else
71    if [ ! -x "$JAVA_HOME"/bin/java ] || [ ! -x "$JAVA_HOME"/bin/jdb ] || [ ! -x "$JAVA_HOME"/bin/javac ]; then
72      echo "The JAVA_HOME environment variable is not defined correctly"
73      echo "This environment variable is needed to run this program"
74      echo "NB: JAVA_HOME should point to a JDK not a JRE"
75      exit 1
76    fi
77  fi
78fi
79
80# Don't override the endorsed dir if the user has set it previously
81if [ -z "$JAVA_ENDORSED_DIRS" ]; then
82  # Java 9 no longer supports the java.endorsed.dirs
83  # system property. Only try to use it if
84  # CATALINA_HOME/endorsed exists.
85  if [ -d "$CATALINA_HOME"/endorsed ]; then
86    JAVA_ENDORSED_DIRS="$CATALINA_HOME"/endorsed
87  fi
88fi
89
90# Set standard commands for invoking Java, if not already set.
91if [ -z "$_RUNJAVA" ]; then
92  _RUNJAVA="$JRE_HOME"/bin/java
93fi
94if [ "$os400" != "true" ]; then
95  if [ -z "$_RUNJDB" ]; then
96    _RUNJDB="$JAVA_HOME"/bin/jdb
97  fi
98fi
99