How to pass the -D additional parameter while starting tomcat?

半世苍凉 提交于 2019-11-29 00:45:38

问题


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?


回答1:


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




回答2:


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"



回答3:


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.




回答4:


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.




回答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


来源:https://stackoverflow.com/questions/14984256/how-to-pass-the-d-additional-parameter-while-starting-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!