tomcat setenv脚本

setenv.bat(linux下是setenv.sh)文件是tomcat的自定义变量脚本文件,该脚本文件放置在CATALINA_HOME\bin\CATALINA_BASE\bin\目录下,该文件需要用户自己创建。在该文件中可以指定java环境变量、jvm参数、catalina参数等。可以设置的参数如下:

变量名 描述
CATALINA_HOME tomcat的安装目录
CATALINA_BASE tomcat实例的工作目录
CATALINA_OPTS startrun或者debug命令执行时的Java虚拟机参数。只能被tomcat自身使用,但在stopversion命令执行时不使用。注意区分JAVA_OPTS
JAVA_HOME Java开发工具安装路径,仅在使用debug参数启动时有用
JRE_HOME Java运行时安装路径,如果该参数为空,将使用JAVA_HOME的值,如果JAVA_HOMEJRE_HOME都设置了,将使用JRE_HOME
JAVA_OPTS 用于执行任何命令时的Java虚拟机参数,既能被tomcat自身使用,也在stopversion命令执行时使用。注意区分CATALINA_OPTS
LOGGING_CONFIG tomcat日志配置变量。例如: set LOGGING_CONFIG="-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties"
LOGGING_MANAGER tomcat日志管理变量。例如: set LOGGING_MANAGER="-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager"

以上只是常用的变量,关于setenv脚本的更多详细内容请参考CATALINA_HOME\bin\目录下的catalina.batcatalina.sh文件中的介绍。 <!-- more -->

setenv.bat文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off
echo ======== setenv start ========
rem 设置JRE根目录
set JRE_HOME=D:\Program Files\Java\jdk1.8.0_102\jre
rem 设置JAVA根目录
set JAVA_HOME=D:\Program Files\Java\jdk1.8.0_102
rem 设置JAVA_OPTS
set JAVA_OPTS=%JAVA_OPTS%
rem 设置CATALINA_OPTS
set CATALINA_OPTS=-Dfile.encoding=UTF-8 -Duser.timezone=GMT+8
echo Using JRE_HOME: %JRE_HOME%
echo Using JAVA_HOME: %JAVA_HOME%
echo Using JAVA_OPTS: %JAVA_OPTS%
echo Using CATALINA_OPTS: %CATALINA_OPTS%
echo ======== setenv over ========

关于CATALINA_HOME 和 CATALINA_BASE

CATALINA_HOME是tomcat的安装目录,CATALINA_BASE是tomcat实例的工作目录。安装目录只有一个,但是实例可以有多个。

CATALINA_BASE包含以下目录和文件:

  • bin - 只包含以下文件:

        * setenv.sh (*nix) or setenv.bat (Windows),
        * tomcat-juli.jar
    
       The setenv scripts were described above. The tomcat-juli library
       is documented in the Logging chapter in the User Guide.
    
  • conf - 服务配置文件目录 (including server.xml)

  • lib - 库文件目录。建议将自己的和第三方的库文件放置在该目录下

  • logs - 日志和输出目录

  • webapps - 自动加载的web应用目录

  • work - web应用的临时工作目录

  • temp - JVM临时文件目录(java.io.tmpdir)

CATALINA_HOME包含以下目录和文件:

  • bin - 启动和关闭脚本文件目录

       当以下文件不在`CATALINA_BASE/bin`目录时将会被使用:
    
       setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar
    
  • lib - 库文件目录。建议只放置标准tomcat库文件

关于CATALINA_HOME 和 CATALINA_BASE可以参考RUNNING.txt
其中关于这两个参数的原文描述如下:

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
==================================================
Advanced Configuration - Multiple Tomcat Instances
==================================================

In many circumstances, it is desirable to have a single copy of a Tomcat
binary distribution shared among multiple users on the same server. To make
this possible, you can set the CATALINA_BASE environment variable to the
directory that contains the files for your 'personal' Tomcat instance.

When running with a separate CATALINA_HOME and CATALINA_BASE, the files
and directories are split as following:

In CATALINA_BASE:

* bin - Only the following files:

* setenv.sh (*nix) or setenv.bat (Windows),
* tomcat-juli.jar

The setenv scripts were described above. The tomcat-juli library
is documented in the Logging chapter in the User Guide.

* conf - Server configuration files (including server.xml)

* lib - Libraries and classes, as explained below

* logs - Log and output files

* webapps - Automatically loaded web applications

* work - Temporary working directories for web applications

* temp - Directory used by the JVM for temporary files (java.io.tmpdir)


In CATALINA_HOME:

* bin - Startup and shutdown scripts

The following files will be used only if they are absent in
CATALINA_BASE/bin:

setenv.sh (*nix), setenv.bat (Windows), tomcat-juli.jar

* lib - Libraries and classes, as explained below

In the default configuration the JAR libraries and classes both in
CATALINA_BASE/lib and in CATALINA_HOME/lib will be added to the common
classpath, but the ones in CATALINA_BASE will be added first and thus will
be searched first.

The idea is that you may leave the standard Tomcat libraries in
CATALINA_HOME/lib and add other ones such as database drivers into
CATALINA_BASE/lib.

tomcat setenv