Mark Comix Mark Comix - 1 year ago 105
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:


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


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 How?. Can someone give me and example?

Thanks and sorry for my english

Answer Source

I don't know what version of Tomcat you using, but in Tomcat 7 in file 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 "" file, and including it in applicationContext like this:

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

And solution for

#   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.




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


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:, and set of files with db connection settings (,, 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


and in applicationConfig.xml

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

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