Mark Comix Mark Comix - 3 months ago 20
Java Question

Pass VM Argument to Apache Tomcat

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

Answer

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" />
Comments