126

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.

Gnanam
  • 10,613
  • 19
  • 54
  • 72

3 Answers3

180

There are two environment variables - CATALINA_OPTS and JAVA_OPTS - which are both used in the catalina.sh startup and shutdown script for Tomcat.

CATALINA_OPTS: Comment inside 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.

So why are there two different variables? And what's the difference?

  1. Firstly, anything specified in EITHER variable is passed, identically, to the command that starts up Tomcat - the start or run command - but only values set in JAVA_OPTS are passed to the stop command. That probably doesn't make any difference to how Tomcat runs in practice as it only effects the END of a run, not the start.

  2. The second difference is more subtle. Other applications may also use JAVA_OPTS, but only Tomcat will use CATALINA_OPTS. So if you're setting environment variables for use only by Tomcat, you'll be best advised to use CATALINA_OPTS, whereas if you're setting environment variables to be used by other java applications as well, such as by JBoss, you should put your settings in JAVA_OPTS.

Source: CATALINA_OPTS v JAVA_OPTS - What is the difference?

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Gnanam
  • 10,613
  • 19
  • 54
  • 72
  • 12
    It's also useful to think of them as "do I need something for tomcat startup" or "do I need something for every JVM". Let's say we are trying to setup JMX monitoring on a distributed environment and we're behind a firewall - we'll need two RMI ports thus to set up Djava.rmi.server as a *startup* arg. Would we do this as a JAVA_OPT a shutdown fires a new JVM which tries to listen on JMX ports, can't grab it as tomcat already has it listening and the JVM will stop with an error about it being already in use - not what we want is it ? – Joao Figueiredo Mar 25 '14 at 09:49
  • Another important difference: [`daemon.sh` erases `JAVA_OPTS`](https://github.com/apache/tomcat/blob/96dec4ac9066687a76874dee4465181182bad055/bin/daemon.sh#L130). So if you're running Tomcat with `jsvc` and you need to pass options to the Java runtime, be sure to set them in `CATALINA_OPTS` instead. – csd May 04 '22 at 13:11
1

I'd like to add that JAVA_OPTS and CATALINA_OPTS are mutually complemental: If you define both environment variables, the content of both will be concatenated and passed to the start and run command - as explained by Gnanam above.

You can also refer to the original source of catalina.sh

Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
-1

Durung shutdown, tomcat launches mutiple vm as explain in comment by @joao. If you are recording some data during tomcat shutdowm, use CATALINA_OPTS not JAVA_OPTS. A great example is when I want save the data during jacoco.exec, I should be using CATALINA_OPTS not JAVA_OPTS.

vsingh
  • 6,365
  • 3
  • 53
  • 57