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

Mark Comix
  • 1,878
  • 7
  • 28
  • 44
  • 1
    I think you should try using [ServletContext](http://docs.oracle.com/javaee/5/api/javax/servlet/ServletContext.html) parameters, since it's a web project. – Gilberto Torrezan Sep 13 '12 at 13:21
  • 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 anyway – Mark Comix Sep 13 '12 at 13:51
  • That's why the web.xml file exists. It is a deployment descriptor, deliberately separate from the code. – user207421 Sep 13 '12 at 22:23

3 Answers3

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" />
user1516873
  • 5,060
  • 2
  • 37
  • 56
  • 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 lot – Mark Comix Sep 13 '12 at 13:52
  • Also, I'm working with Windows. Do i have to use catalina.sh or catalina.BAT? – Mark Comix Sep 13 '12 at 13:55
  • 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) – user1516873 Sep 13 '12 at 14:00
  • for windows catalina.bat – user1516873 Sep 13 '12 at 14:00
  • I use Eclipse to generate the WAR file. I'm trying your solution and isn't working :( – Mark Comix Sep 13 '12 at 14:03
  • Can you give me more details of your other Solution. Maybe I can use later. Thanks – Mark Comix Sep 13 '12 at 14:10
  • You don't using any build system, so different solutions isn't any help in this situation. – user1516873 Sep 13 '12 at 14:34
  • 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? Thanks – Mark Comix Sep 13 '12 at 14:48
  • 1
    for windows it sould be `set CATALINA_OPTS="-Dfolder=Dev"`, not `CATALINA_OPTS="-Dfolder=Dev"` – user1516873 Sep 13 '12 at 14:55
  • 1
    Comments should already be there, i copied it from original catalina.sh. – user1516873 Sep 13 '12 at 14:58
  • Still not working. Is not replacing ${folder} with the CATALINA_OPTS="-Dfolder=Dev" value inside catalina.bat file. I'm stating to desperate – Mark Comix Sep 13 '12 at 15:07
  • 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 work – Mark Comix Sep 13 '12 at 15:27
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
Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
biniam
  • 8,099
  • 9
  • 49
  • 58
  • 2
    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. – Sebastian May 10 '17 at 18:53
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

Hkachhia
  • 4,463
  • 6
  • 41
  • 76
Osmar
  • 557
  • 6
  • 10