How tomcat works 读书笔记十七 启动tomcat 下

网友投稿 268 2022-10-30


How tomcat works 读书笔记十七 启动tomcat 下

在上一节中,我们程序的起始位置还是Bootstrap,现在我们通过bat文件来启动这个类。 在分析catalina.bat之前,我们先看看几个简单的我们能用到的dos命令。

基础知识

1 rem 注释,解释器不会执行rem开始的命令

2 pause 用于暂停正在执行的批处理文件,按任意键后继续

3 echo 在控制台显示一段文本

如果显示变量的值需要在变量前后加% 如echo %var% 如果要输出操作系统的名字 就是 echo %os%

4 echo off 可以将命令本身隐藏 如果要连echo off 本身也要隐藏 就是@echo off

5 set 设置一个变量

可以看下面的一个例子

@echo off

echo 你好呀?

pause

set a=15

echo %a%

echo 再按就退出

echo %os%

pause

6 label 使用:标识一个标签,和goto合起来使用

7 goto 跳转到相应的label处

看下面的例子

echo start

goto end

echo 你看不到我

:end

echo endss

pause

运行上面的命令后,控制台上只会出现

start

endss

8 if 执行条件测试

有三种用法

测试变量的值

set var=15

if %var%=15 echo 是15

测试文件是否存在

if exist c:/file.text echo 存在文件

9 not 取反

set var=15

if not %var%=15 echo 不是15

10 接收参数

用%1表示第一个参数 %2表示第二个 以此类推

如果有一个ss.bat里面如下

echo %1

在控制台用下面的命令调用ss

ss hello

就会输出第一个参数hello

11 shift

将参数后移 等于是把%2复制给了%1;%3给了%2

12 call 调用另一条命令

13 setLocal 使得对变量的修改只在本bat文件内有效 bat文件结束后变量恢复原值

14 start 打开一个新的窗口

start name command

name是新的窗口的名字 后面带着的是在新窗口中要执行的命令

当然也可以没有命令

在tomcat的安装目录下有bin文件夹

cd进去后 在命令行形式下运行catalina 就可以运行bat文件了

不过在运行的时候 还需要一些参数

下来激动人心的时刻到了 看源码

我们就按照调用 catalina start 来看代码

我用//来做注释

Catalina.bat

@echo off //隐藏命令rem Licensed to the Apache Software Foundation (ASF) under one or morerem contributor license agreements. See the NOTICE file distributed withrem this work for additional information regarding copyright ownership.rem The ASF licenses this file to You under the Apache License, Version 2.0rem (the "License"); you may not use this file except in compliance withrem the License. You may obtain a copy of the License atremrem Unless required by applicable law or agreed to in writing, softwarerem distributed under the License is distributed on an "AS IS" BASIS,rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.rem See the License for the specific language governing permissions andrem limitations under the License.//上面的是注释 不管 os就是windows_NT setlocal上面有if "%OS%" == "Windows_NT" setlocalrem ---------------------------------------------------------------------------rem Start/Stop Script for the CATALINA Serverremrem Environment Variable Prerequisitesremrem Do not set the variables in this script. Instead put them into a scriptrem setenv.bat in CATALINA_BASE/bin to keep your customizations separate.remrem CATALINA_HOME May point at your Catalina "build" directory.remrem CATALINA_BASE (Optional) Base directory for resolving dynamic portionsrem of a Catalina installation. If not present, resolves torem the same directory that CATALINA_HOME points to.remrem CATALINA_OPTS (Optional) Java runtime options used when the "start",rem "run" or "debug" command is executed.rem Include here and not in JAVA_OPTS all options, that shouldrem only be used by Tomcat itself, not by the stop process,rem the version command etc.rem Examples are heap size, GC logging, JMX ports etc.remrem CATALINA_TMPDIR (Optional) Directory path location of temporary directoryrem the JVM should use (java.io.tmpdir). Defaults torem %CATALINA_BASE%\temp.remrem JAVA_HOME Must point at your Java Development Kit installation.rem Required to run the with the "debug" argument.remrem JRE_HOME Must point at your Java Runtime installation.rem Defaults to JAVA_HOME if empty. If JRE_HOME and JAVA_HOMErem are both set, JRE_HOME is used.remrem JAVA_OPTS (Optional) Java runtime options used when any commandrem is executed.rem Include here and not in CATALINA_OPTS all options, thatrem should be used by Tomcat and also by the stop process,rem the version command etc.rem Most options should go into CATALINA_OPTS.remrem JAVA_ENDORSED_DIRS (Optional) Lists of of semi-colon separated directoriesrem containing some jars in order to allow replacement of APIsrem created outside of the JCP (i.e. DOM and SAX from W3C).rem It can also be used to update the XML parser implementation.rem Defaults to $CATALINA_HOME/endorsed.remrem JPDA_TRANSPORT (Optional) JPDA transport used when the "jpda start"rem command is executed. The default is "dt_socket".remrem JPDA_ADDRESS (Optional) Java runtime options used when the "jpda start"rem command is executed. The default is 8000.remrem JPDA_SUSPEND (Optional) Java runtime options used when the "jpda start"rem command is executed. Specifies whether JVM should suspendrem execution immediately after startup. Default is "n".remrem JPDA_OPTS (Optional) Java runtime options used when the "jpda start"rem command is executed. If used, JPDA_TRANSPORT, JPDA_ADDRESS,rem and JPDA_SUSPEND are ignored. Thus, all required jpdarem options MUST be specified. The default is:remrem -agentlib:jdwp=transport=%JPDA_TRANSPORT%,rem address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%remrem LOGGING_CONFIG (Optional) Override Tomcat's logging config filerem Example (all one line)rem set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"remrem LOGGING_MANAGER (Optional) Override Tomcat's logging managerrem Example (all one line)rem set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"remrem TITLE (Optional) Specify the title of Tomcat window. The defaultrem TITLE is Tomcat if it's not specified.rem Example (all one line)rem set TITLE=Tomcat.Cluster#1.Server#1 [%DATE% %TIME%]remremremrem $Id: catalina.bat 1344732 2012-05-31 14:08:02Z kkolinko $rem ---------------------------------------------------------------------------rem Suppress Terminate batch job on CTRL+Cif not ""%1"" == ""run"" goto mainEntry //如果第一个参数不是run 去mainEntry标签 11行后if "%TEMP%" == "" goto mainEntryif exist "%TEMP%\%~nx0.run" goto mainEntryecho Y>"%TEMP%\%~nx0.run"if not exist "%TEMP%\%~nx0.run" goto mainEntryecho Y>"%TEMP%\%~nx0.Y"call "%~f0" %* <"%TEMP%\%~nx0.Y"rem Use provided errorlevelset RETVAL=%ERRORLEVEL%del /Q "%TEMP%\%~nx0.Y" >NUL 2>&1exit /B %RETVAL%:mainEntry //看不懂del /Q "%TEMP%\%~nx0.run" >NUL 2>&1rem Guess CATALINA_HOME if not definedset "CURRENT_DIR=%cd%" //cd就是当前路径if not "%CATALINA_HOME%" == "" goto gotHome //不等于空 就去gotHomeset "CATALINA_HOME=%CURRENT_DIR%" //要是等于空 就认为catalina.bat所在的文件夹就是 //CATALINA_HOMEif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomecd .. //判定CATALINA_HOME里是不是存在bin\catalina.bat //如果不存在 返回上一级目录 //要记得正常情况下 catalina.bat本来就在bin目录下set "CATALINA_HOME=%cd%"cd "%CURRENT_DIR%":gotHome if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHome //到这里就好了 存在这个bat文件 去okhome //看看echo The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHomerem Copy CATALINA_BASE from CATALINA_HOME if not definedif not "%CATALINA_BASE%" == "" goto gotBase //英文注释说的很清楚了set "CATALINA_BASE=%CATALINA_HOME%":gotBaserem Ensure that any user defined CLASSPATH variables are not used on startup,rem but allow them to be specified in setenv.bat, in rare case when it is needed.set CLASSPATH=rem Get standard environment variables //就是调用setenv.batif not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHomecall "%CATALINA_BASE%\bin\setenv.bat" goto setenvDone:checkSetenvHomeif exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat":setenvDonerem Get standard Java environment variables //调用setclasspathif exist "%CATALINA_HOME%\bin\setclasspath.bat" goto okSetclasspathecho Cannot find "%CATALINA_HOME%\bin\setclasspath.bat"echo This file is needed to run this programgoto end:okSetclasspathset BASEDIR=%CATALINA_HOME%call "%CATALINA_HOME%\bin\setclasspath.bat"//setclasspath.bat 会检查环境变量java_home//并且会设置以下的变量//set JAVA_ENDORSED_DIRS=%BASEDIR%\common\endorsed//set CLASSPATH=%JAVA_HOME%\lib\tools.jar//set _RUNJAVA="%JAVA_HOME%\bin\java"//set _RUNJAVAW="%JAVA_HOME%\bin\javaw"//set _RUNJDB="%JAVA_HOME%\bin\jdb"//set _RUNJAVAC="%JAVA_HOME%\bin\javac"rem Add on extra jar files to CLASSPATHif "%JSSE_HOME%" == "" goto noJsse //看英文注释set CLASSPATH=%CLASSPATH%;%JSSE_HOME%\lib\jcert.jar;%JSSE_HOME%\lib\jnet.jar;%JSSE_HOME%\lib\jsse.jar:noJsseset CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\bin\bootstrap.jarif not "%CATALINA_BASE%" == "" goto gotBaseset CATALINA_BASE=%CATALINA_HOME%:gotBaseif not "%CATALINA_TMPDIR%" == "" goto gotTmpdirset CATALINA_TMPDIR=%CATALINA_BASE%\temp:gotTmpdirrem ----- Execute The Requested Command ---------------------------------------echo Using CATALINA_BASE: "%CATALINA_BASE%"echo Using CATALINA_HOME: "%CATALINA_HOME%"echo Using CATALINA_TMPDIR: "%CATALINA_TMPDIR%"if ""%1"" == ""debug"" goto use_jdkecho Using JRE_HOME: "%JRE_HOME%"goto java_dir_displayed:use_jdkecho Using JAVA_HOME: "%JAVA_HOME%":java_dir_displayedecho Using CLASSPATH: "%CLASSPATH%"set _EXECJAVA=%_RUNJAVA%set MAINCLASS=org.apache.catalina.startup.Bootstrapset ACTION=startset SECURITY_POLICY_FILE=set DEBUG_OPTS=set JPDA=if not ""%1"" == ""jpda"" goto noJpda //第一个参数是start 去看nojpda 15行后set JPDA=jpdaif not "%JPDA_TRANSPORT%" == "" goto gotJpdaTransportset JPDA_TRANSPORT=dt_socket:gotJpdaTransportif not "%JPDA_ADDRESS%" == "" goto gotJpdaAddressset JPDA_ADDRESS=8000:gotJpdaAddressif not "%JPDA_SUSPEND%" == "" goto gotJpdaSuspendset JPDA_SUSPEND=n:gotJpdaSuspendif not "%JPDA_OPTS%" == "" goto gotJpdaOptsset JPDA_OPTS=-agentlib:jdwp=transport=%JPDA_TRANSPORT%,address=%JPDA_ADDRESS%,server=y,suspend=%JPDA_SUSPEND%:gotJpdaOptsshift:noJpdaif ""%1"" == ""debug"" goto doDebugif ""%1"" == ""run"" goto doRunif ""%1"" == ""start"" goto doStart //呵呵if ""%1"" == ""stop"" goto doStopif ""%1"" == ""configtest"" goto doConfigTestif ""%1"" == ""version"" goto doVersionecho Usage: catalina ( commands ... )echo commands:echo debug Start Catalina in a debuggerecho debug -security Debug Catalina with a security managerecho jpda start Start Catalina under JPDA debuggerecho run Start Catalina in the current windowecho run -security Start in the current window with security managerecho start Start Catalina in a separate windowecho start -security Start in a separate window with security managerecho stop Stop Catalinaecho configtest Run a basic syntax check on server.xmlecho version What version of tomcat are you running?goto end:doDebugshiftset _EXECJAVA=%_RUNJDB%set DEBUG_OPTS=-sourcepath "%CATALINA_HOME%\..\..\java"if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doRunshiftif not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStartshift //移动参数if not "%OS%" == "Windows_NT" goto noTitle //刚才已经说了 os就是Windows_NTif "%TITLE%" == "" set TITLE=Tomcatset _EXECJAVA=start "%TITLE%" %_RUNJAVA% //title是tomcatgoto gotTitle //_EXECJAVA这就是以后的命令:noTitleset _EXECJAVA=start %_RUNJAVA%:gotTitle //之前移动了参数了 但是我们确实没有第二个参数 去看execCmd 24行后if not ""%1"" == ""-security"" goto execCmdshiftecho Using Security Managerset "SECURITY_POLICY_FILE=%CATALINA_BASE%\conf\catalina.policy"goto execCmd:doStopshiftset ACTION=stopset CATALINA_OPTS=goto execCmd:doConfigTestshiftset ACTION=configtestset CATALINA_OPTS=goto execCmd:doVersion%_EXECJAVA% -classpath "%CATALINA_HOME%\lib\catalina.jar" org.apache.catalina.util.ServerInfogoto end:execCmdrem Get remaining unshifted command line arguments and save them in theset CMD_LINE_ARGS=:setArgs //此时的第一个参数为空 所以去看doneSetArgsif ""%1""=="""" goto doneSetArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setArgs:doneSetArgsrem Execute Java with the applicable propertiesif not "%JPDA%" == "" goto doJpda //两个变量都为空if not "%SECURITY_POLICY_FILE%" == "" goto doSecurity%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%// 在我的电脑上将上面的变量赋值后就是//start "Tomcat" "C:\j2sdk1.4.2_02\bin\java" -//Djava.endorsed.dirs="..\common\endorsed" -classpath//"C:\j2sdk1.4.2_02\lib\tools.jar;..\bin\bootstrap.jar" -//Dcatalina.base=".." -Dcatalina,home=".." -Djava.io.tmpdir="..\temp"//org.apache.catalina.startup.Bootstrap start//解释一下//启动一个新的窗口叫tomcat 然后就调用java命令 从-Djava到/temp都是参数//运行的是org.apache.catalina.startup.Bootstrap 参数是start//我累个去 重要写完啦//感谢gltgoto end //去看end:doSecurity%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%goto end:doJpdaif not "%SECURITY_POLICY_FILE%" == "" goto doSecurityJpda%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%goto end:doSecurityJpda%_EXECJAVA% %JAVA_OPTS% %CATALINA_OPTS% %JPDA_OPTS% %DEBUG_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%" -classpath "%CLASSPATH%" -Djava.security.manager -Djava.security.policy=="%SECURITY_POLICY_FILE%" -Dcatalina.base="%CATALINA_BASE%" -Dcatalina.home="%CATALINA_HOME%" -Djava.io.tmpdir="%CATALINA_TMPDIR%" %MAINCLASS% %CMD_LINE_ARGS% %ACTION%goto end:end //对end在这

其实还没有到最开始的那个类

当当当当~~~

Startup.bat

@echo offif "%OS%" == "Windows_NT" setlocalrem ---------------------------------------------------------------------------rem Start script for the CATALINA Serverremrem $Id: startup.bat,v 1.4 2002/01/15 02:55:38 patrickl Exp $rem ---------------------------------------------------------------------------rem Guess CATALINA_HOME if not definedif not "%CATALINA_HOME%" == "" goto gotHomeset CATALINA_HOME=. // 一个点是当前目录if exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeset CATALINA_HOME=.. //两个点事上级目录 哎 今天涨姿势了:gotHome//上一级目录里面是有bin文件夹的 所以去okhomeif exist "%CATALINA_HOME%\bin\catalina.bat" goto okHomeecho The CATALINA_HOME environment variable is not defined correctlyecho This environment variable is needed to run this programgoto end:okHomeset EXECUTABLE=%CATALINA_HOME%\bin\catalina.batrem Check that target executable existsif exist "%EXECUTABLE%" goto okExec //EXECUTABLE肯定有呀 去看okExececho Cannot find %EXECUTABLE%echo This file is needed to run this programgoto end:okExecrem Get remaining unshifted command line arguments and save them in theset CMD_LINE_ARGS=:setArgsif ""%1""=="""" goto doneSetArgs //这里没有参数 所以去doneSetArgsset CMD_LINE_ARGS=%CMD_LINE_ARGS% %1shiftgoto setArgs:doneSetArgscall "%EXECUTABLE%" start %CMD_LINE_ARGS% //调用bin\catalina.bat并且参数为start:end

tomcat终于结束了 感谢glt


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java栈实现二叉树的非递归遍历的示例代码
下一篇:tomcat中的线程问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~