29

I have a maven project, after running command mvn install all project as well as module compile and copied to local repository successfully. But now I want to run the generated web application in tomcat6. Client provided some parameter for tomcat like -Dapp.username,-Dapp.username, which will be used internally once project will start.ButI do not know how to set these additional parameter in tomcat6. Below is my development environment

  1. OS = Windows
  2. Tomcat = tomcat 6.0.27

Please help me?

Rais Alam
  • 6,970
  • 12
  • 53
  • 84

6 Answers6

41

For Tomcat 6 you should add the params to the startup.sh (Windows startup.bat). For Tomcat 7 and above you should set the parameters in the {Catalina Root}/bin/setenv.sh like such:

export CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"

Or in Windows:

set CATALINA_OPTS="$CATALINA_OPTS -Dapp.username=username -Dapp.password=password"

NOTE: Notice the $CATALINA_OPTS at the beginning so you don't wipe out any previously set values. Not doing so can create a very hard to debug problem!

If the parameters you are setting are solely to be used by Tomcat then be sure to set it using CATALINA_OPTS.

If your application will be using the parameters then be sure to use JAVA_OPTS instead. Tomcat will also read these parameters. This can also go in the setenv.sh file. For instance:

export JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"

Or in Windows:

set JAVA_OPTS="$JAVA_OPTS -Dapp.username=username -Dapp.password=password"
DavidR
  • 6,622
  • 13
  • 56
  • 70
  • 3
    this answer needs more upvotes. catalina_opts is more neat – Monish Sen Jul 28 '16 at 06:46
  • 1
    use catalina_opts whenever possible – viz Jun 02 '17 at 06:33
  • Just verified this after gave me a quick chase: do NOT include the quotes in Windows and use % instead of $. So it should look like:set CATALINA_OPTS=%CATALINA_OPTS% -Dapp.username=username -Dapp.password=password (Tomcat 8, Windows 10, Java 8) – apil.tamang Dec 10 '19 at 17:01
  • useful link: https://docs.oracle.com/cd/E40520_01/integrator.311/integrator_install/src/cli_ldi_server_config.html#:~:text=Apache%20Tomcat%20does%20not%20run,variable%20in%20the%20setenv%20file. – andreagalle Jun 17 '22 at 12:08
19

You can set an environment variable to do that. E.g. in Linux:

export JAVA_OPTS="-Dapp.username -Dapp.username"

Or in Windows:

set JAVA_OPTS="-Dapp.username -Dapp.username"

Do this before starting Tomcat

betomontejo
  • 1,657
  • 14
  • 26
  • 7
    I would do `export JAVA_OPTS="${JAVA_OPTS} -Dapp.username -Dapp.username"` just to be safe. – Boris the Spider Feb 20 '13 at 16:00
  • 1
    but where do i set these variable. – Rais Alam Feb 20 '13 at 16:10
  • How are you starting tomcat? In a console with startup.bat? As a service or daemon? – betomontejo Feb 20 '13 at 16:14
  • Yes with the help of bin/startup.bat – Rais Alam Feb 20 '13 at 16:15
  • You can set either variable by doing the "SET" command in the same console window from which you launch startup.bat, or you can go all the way to the Windows Environment Variable settings and do it there, whichever you prefer – betomontejo Feb 20 '13 at 16:18
  • Many applications use the JAVA_OPTS environment variable, so you could inadvertently mess up some other application. You are better off setting CATALINA_OPTS, which is more specific to Tomcat. – kc2001 Mar 14 '18 at 17:42
8

You will want to set the CATALINA_OPTS system variable - this is read by Tomcat (and only by Tomcat) when starting. As @Betoverse says you can set this using the two methods:

export CATALINA_OPTS="-Dapp.username -Dapp.username"

Or in Windows:

set CATALINA_OPTS="-Dapp.username -Dapp.username"

You can add that command to your ~/.profile on UNIX to have it set automatically.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • I am working with windows. Where do i set this variable. In windows environment variable or some where else. – Rais Alam Feb 20 '13 at 16:10
  • 1
    On windows it's an environment variable, yes. [Here is a guide](http://www.itechtalk.com/thread3595.html) on setting them. Or you can edit `startup.bat` as [here](http://www.springwebdevelopment.com/tomcat-more-memory-catalina_opts) - that's messier however. – Boris the Spider Feb 20 '13 at 16:13
6

I have tested params for Tomcat 7/8 on Windows 10 and CentOs 7 (Linux).
1) On Windows need to create setenv.bat in the {TOMCAT_HOME}/bin/ path and insert there such code:

set CATALINA_OPTS=-Dapp.username=admin -Dapp.password=12345

IMPORTANT: do not use quotes (" ") for setting params on windows.

2) On CentOs need to create setenv.sh in the {TOMCAT_HOME}/bin/ path and insert there such code:

export CATALINA_OPTS="-Dapp.username=admin -Dapp.password=12345"

You can also create {TOMCAT_HOME}/conf/conf.d/custom.conf and insert the same export command there.

Maksym
  • 2,650
  • 3
  • 32
  • 50
  • My IT staffer installed Tomcat 8 on CentOS/Linux and I can't find any reference to 'setenv.sh' - in other words, there's no place that I could find where that script gets run if it exists. Also, there's no 'catalina.sh' that I can find anywhere. Do I have a non-standard install of Tomcat 8? – rich p Jul 03 '19 at 17:04
  • 1
    you can create setenv.sh by yourself. By default it is just empty file. On CentOs usually the default path is /usr/share/tomcat8/bin/setenv.sh ( $CATALINA_BASE/bin). I guess your tomcat was installed by package manager (e.g yum) and was configured as service by default. So it is standard tomcat from package manager. (https://stackoverflow.com/questions/12280372/where-can-i-find-the-tomcat-7-installation-folder-on-linux-ami-in-elastic-beanst) – Maksym Jul 04 '19 at 07:22
  • 1
    useful link https://docs.oracle.com/cd/E40520_01/integrator.311/integrator_install/src/cli_ldi_server_config.html#:~:text=Apache%20Tomcat%20does%20not%20run,variable%20in%20the%20setenv%20file. – andreagalle Jun 17 '22 at 12:09
5

If you don't want to change your environments or edit the .sh files you can start the server with something like the following

CATALINA_OPTS="-Dparam1=value1 -Dparam2=value2" catalina.sh start
Terry Horner
  • 497
  • 6
  • 16
0

before starting tomcat server right click project --> Run as --> Run Configurations second tab --> -Dname=values , -Dname=values , -Dname=values

what about +Dname=value ,, values it become encrypted

Sravan
  • 1
  • 1