CATALINA_OPTS vs JAVA_OPTS - What is the difference?

I was trying to find out the difference between Apache Tomcat variables - CATALINA_OPTS and JAVA_OPTS in SO and surprised to see that there is no question/answer posted here yet. So I thought of sharing it here (with answer) after finding out the difference. Check the answer/difference below.

NOTE: At the time of this posting, we're running Apache Tomcat v6.0.10 with JDK 6u32 on CentOS5 64-bit arch.

91157 次浏览

有两个环境变量-CATALINA_OPTSJAVA_OPTS-它们都用于 Tomcat 的 catalina.sh启动和关闭脚本。

CATALINA _ OPTS : 在 catalina.sh 中发表评论:

#   CATALINA_OPTS   (Optional) Java runtime options used when the "start",
#                   "run" or "debug" command is executed.
#                   Include here and not in JAVA_OPTS all options, that should
#                   only be used by Tomcat itself, not by the stop process,
#                   the version command etc.
#                   Examples are heap size, GC logging, JMX ports etc.

JAVA _ OPTS : Comment inside catalina.sh:

#   JAVA_OPTS       (Optional) Java runtime options used when any command
#                   is executed.
#                   Include here and not in CATALINA_OPTS all options, that
#                   should be used by Tomcat and also by the stop process,
#                   the version command etc.
#                   Most options should go into CATALINA_OPTS.

那么为什么会有两个不同的变量呢? 有什么区别呢?

  1. 首先,任意一个变量中指定的任何内容都同样地传递给启动 Tomcat 的命令—— startrun命令——但是只有在 JAVA_OPTS中设置的值才传递给 stop命令。这可能不会对 Tomcat 在实践中的运行方式产生任何影响,因为它只会影响运行的结束,而不会影响开始。

  2. 第二个区别更加微妙。其他应用程序也可以使用 JAVA_OPTS,但只有 Tomcat 将使用 CATALINA_OPTS。因此,如果您设置的环境变量只能由 Tomcat 使用,那么最好使用 CATALINA_OPTS,而如果您设置的环境变量也可以由其他 Java 应用程序使用,比如 JBoss,那么您应该将设置放在 JAVA_OPTS中。

Source: CATALINA_OPTS v JAVA_OPTS - What is the difference?

在关闭期间,tomcat 启动了多个 vm,如@joao 在评论中解释的那样。 如果您在 tomcat 关闭期间记录一些数据,请使用 CATALINA _ OPTS 而不是 JAVA _ OPTS。一个很好的例子是,当我想在 jacoco.exec 期间保存数据时,我应该使用 CATALINA _ OPTS 而不是 JAVA _ OPTS。

我想补充的是,JAVA_OPTSCATALINA_OPTS相辅相成: 如果您定义了这两个环境变量,content of both will be concatenated并传递给 startrun命令-正如上面 Gnanam 所解释的。

你也可以参考 Catalina.sh 的原始来源