Pass VM Argument to Apache Tomcat [duplicate]
Asked Answered
F

3

30

I have a webProject with a VM Argument called "-Dfolder"

I use that argument on applicationContext like this:

<value>file:${FNET_CORE_CONFIG}/conf/${folder}/jdbc.properties</value>

In Eclipse, for testing, i use "Run Configuration" to set the value like this:

-Dfolder=Dev

Now, I want to test my webapp on Apache Tomcat so I need to set/send the folder VM Argument.

How I do that?

I have to use setenv.sh? How?. Can someone give me and example?

Thanks and sorry for my english

Fund answered 13/9, 2012 at 13:15 Comment(3)
I think you should try using ServletContext parameters, since it's a web project.Consequently
I think that this is no what I need. I need to set the value on apache for the WAR, not in develop. Also I need to change the value time to time for Develop, Test and Demo enviroment. Thanks anywayFund
That's why the web.xml file exists. It is a deployment descriptor, deliberately separate from the code.Sailor
S
29

I don't know what version of Tomcat you using, but in Tomcat 7 in file catalina.sh you can specify variable CATALINA_OPTS and this variable will pass to jvm.

But maybe setting environment variable isn't a best way to achive your goal. Maybe best will be creation of separate "app.properties" file, and including it in applicationContext like this:

<context:property-placeholder location="classpath*:app.properties" />

And solution for 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.

example:

CATALINA_OPTS="-Dfolder=Dev"

EDIT:

for windows it should be something like set CATALINA_OPTS="-Dfolder=Dev"

EDIT:

In Spring configuration you can use system property just like ${propertyname}, and also can include file with property definition, with context:property-placeholder, and all defined in that file properties also become avaliable in config.

For example, you have base set properties: config.properties, and set of files with db connection settings (DEV.properties, UAT.properties, PROD.properties). So, how can you include different properties for different environment? It can be done it many ways, for example, set system properties in catalina.bat

set CATALINA_OPTS="-Dbuild=DEV"

and in applicationConfig.xml

<context:property-placeholder location="classpath*:${build}.properties, classpath*:config.properties" />

Or you can create different build configuration and include in final WAR only one properties (DEV, UAT, PROD) for each build configuration. In applicationConfig set something like:

<context:property-placeholder location="classpath*:*.properties" />
Siret answered 13/9, 2012 at 13:50 Comment(12)
I'm working with Apache Tomcat 7. I'm using VM Argument becouse I need to read a "part" of the path for the Log4j.properties and JDBC.properties. So I try with applicationContext and can't made it work. I will try your answer right now. thanks a lotFund
Also, I'm working with Windows. Do i have to use catalina.sh or catalina.BAT?Fund
Do you use maven or ant to build? One more solution - create different build configuration for different environment, and set all .properties you need in build process. I know Jenkins allow easily configure this. (If you using Jenkins as CI server)Siret
for windows catalina.batSiret
I use Eclipse to generate the WAR file. I'm trying your solution and isn't working :(Fund
Can you give me more details of your other Solution. Maybe I can use later. ThanksFund
You don't using any build system, so different solutions isn't any help in this situation.Siret
Ok. So, I crearte a catalina.bat Then I write CATALINA_OPTS="-Dfolder=Dev" like you say (even write the comentaries). Then I copied the catalina.bat to ..\Apache Software Foundation\Tomcat 7.0\bin. Finally I run apache, and didn't work. The localhost.2012-09-13.log file say ..Could not load properties; nested exception is java.io.FileNotFoundException.... Any idea? ThanksFund
for windows it sould be set CATALINA_OPTS="-Dfolder=Dev", not CATALINA_OPTS="-Dfolder=Dev"Siret
Comments should already be there, i copied it from original catalina.sh.Siret
Still not working. Is not replacing ${folder} with the CATALINA_OPTS="-Dfolder=Dev" value inside catalina.bat file. I'm stating to desperateFund
Finally I made it work. But Not using the catalina.bat. I don't know why, but I executed the Tomcat7w.exe and added the -Dfolder=Dev there and when I started the Windows Server and work!. Anyway, your help, your answers really help me. thanks a lot. I was trying for 3 days to made this workFund
D
20

Go to $CATALINA_HOME and edit setenv.sh file by adding the parameters with the value. If you want to mass multiple parameters, separate them using space

E.g.

cd /opt/tomcat/bin/ 
sudo nano setenv.sh 

edit the line

CATALINA_OPTS="${CATALINA_OPTS}" 

to

CATALINA_OPTS="${CATALINA_OPTS} -Dparam=value -Dparam2=value2" 

restart tomcat:

service tomcat restart

you should now be able to see the arguments passed to tomcat when you run:

ps aux | grep tomcat
Doelling answered 16/12, 2015 at 12:15 Comment(1)
Upvoting because this user showed an example with multiple parameters. I'm used to using the Tomcat configuration gui and it's not obvious from there that the parameters are space delimited.Doldrums
K
6

Made it work in Windows, by generating a setenv.bat file in the same directory as catalina.bat and startup.bat (as recommended in catalina.bat) and put in the contents of the .bat:

set CATALINA_OPTS="-DyourVariableName=yourValue"

That's all. I liked this way as it looks pretty clean

Kindig answered 23/8, 2016 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.