Tomcat啟動分析3_setclasspath.sh

Tomcat8.5.9啟動流程分析3

setclasspath.sh 腳本

#!/bin/sh

#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -----------------------------------------------------------------------------
#  Set JAVA_HOME or JRE_HOME if not already set and ensure any provided
#  settings are valid and consistent with the selected start-up options.
# -----------------------------------------------------------------------------

# Make sure prerequisite environment variables are set
# 如果JAVA_HOME和JRE_HOME都為空,則設(shè)置JAVA_HOME JRE_HOME環(huán)境變量
if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
  # MacOS的JAVA路徑不同于普通的linux,所以特殊處理來獲取JAVA_HOME
  if $darwin; then
    # Bugzilla 54390
    if [ -x '/usr/libexec/java_home' ] ; then
      export JAVA_HOME=`/usr/libexec/java_home`
    # Bugzilla 37284 (reviewed).
    elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
      export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
    fi
  else
    # 通過which命令來獲取java可執(zhí)行文件的位置 "2>/dev/null"表示過濾錯誤輸出
    JAVA_PATH=`which java 2>/dev/null`
    if [ "x$JAVA_PATH" != "x" ]; then
    # JAVA_PATH非空,則通過dirname獲取其所在文件夾路徑賦值給JAVA_PATH以及JRE_HOME
      JAVA_PATH=`dirname $JAVA_PATH 2>/dev/null`
      JRE_HOME=`dirname $JAVA_PATH 2>/dev/null`
    fi
    # 如果JRE_HOME變量沒有設(shè)置,且存在/usr/bin/java可執(zhí)行文件,則默認(rèn)JRE_HOME設(shè)置為/usr
    if [ "x$JRE_HOME" = "x" ]; then
      # XXX: Should we try other locations?
      if [ -x /usr/bin/java ]; then
        JRE_HOME=/usr
      fi
    fi
  fi
  # 如果到這一步JAVA_HOME都JRE_HOME都為空,則直接退出
  if [ -z "$JAVA_HOME" -a -z "$JRE_HOME" ]; then
    echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
    echo "At least one of these environment variable is needed to run this program"
    exit 1
  fi
fi
# 判斷是否以debug模式啟動tomcat,如果是debug模式則要求JAVA_HOME環(huán)境變量不能為空,否則直接退出
if [ -z "$JAVA_HOME" -a "$1" = "debug" ]; then
  echo "JAVA_HOME should point to a JDK in order to run in debug mode."
  exit 1
fi
# 如果JRE_HOME環(huán)境變量為空,則默認(rèn)賦值JAVA_HOME的值
if [ -z "$JRE_HOME" ]; then
  JRE_HOME="$JAVA_HOME"
fi

# If we're running under jdb, we need a full jdk.
# 如果debug模式啟動tomcat則需要完整jdk而不僅僅jre,所以需要判斷對應(yīng)的可執(zhí)行文件是否存在
# jdb用于debug模式啟動tomcat
if [ "$1" = "debug" ] ; then
  if [ "$os400" = "true" ]; then
    if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/javac ]; then
      echo "The JAVA_HOME environment variable is not defined correctly"
      echo "This environment variable is needed to run this program"
      echo "NB: JAVA_HOME should point to a JDK not a JRE"
      exit 1
    fi
  else
    if [ ! -x "$JAVA_HOME"/bin/java -o ! -x "$JAVA_HOME"/bin/jdb -o ! -x "$JAVA_HOME"/bin/javac ]; then
      echo "The JAVA_HOME environment variable is not defined correctly"
      echo "This environment variable is needed to run this program"
      echo "NB: JAVA_HOME should point to a JDK not a JRE"
      exit 1
    fi
  fi
fi

# Set standard commands for invoking Java, if not already set.
# 設(shè)置_RUNJAVA _RUNJDB變量值,之后用于啟動org.apache.catalina.startup.Bootstrap
# _RUNJAVA使用java命令啟動
# _RUNJDB使用jdb命令啟動
if [ -z "$_RUNJAVA" ]; then
  _RUNJAVA="$JRE_HOME"/bin/java
fi
if [ "$os400" != "true" ]; then
  if [ -z "$_RUNJDB" ]; then
    _RUNJDB="$JAVA_HOME"/bin/jdb
  fi
fi

if判斷 為ture的條件
-r FILE 文件存在且可讀
-w FILE 文件存在且可寫
-x FILE 文件存在且可執(zhí)行
-z String 字符串為空
-n String 字符串非空
-f FILE 文件存在且是普通文件
-s FILE 文件存在且大小非0
-S FILE 文件存在且為套接字文件
-a FILE 文件存在
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容