Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass VM Argument to Apache Tomcat [duplicate]

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

like image 752
Mark Comix Avatar asked Sep 13 '12 13:09

Mark Comix


People also ask

How Tomcat ClassLoader works?

Like many server applications, Tomcat installs a variety of class loaders (that is, classes that implement java. lang. ClassLoader ) to allow different portions of the container, and the web applications running on the container, to have access to different repositories of available classes and resources.

What is classpath in Tomcat?

A classpath is an argument that tells the JVM where to find the classes and packages necessary to run a program. The classpath is always set from a source outside the program itself.


Video Answer


2 Answers

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" /> 
like image 194
user1516873 Avatar answered Oct 11 '22 04:10

user1516873


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 
like image 27
biniam Avatar answered Oct 11 '22 03:10

biniam