Pass VM Argument to Apache Tomcat [duplicate] - java

This question already has answers here:
How to pass the -D additional parameter while starting tomcat?
(6 answers)
Closed 5 years ago.
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

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

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

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

Related

How to set custom environment variables in tomcat?

I have few key- value pair variables in my program which is hard coded now.
String pswd = StringUtils.defaultString(System.getProperty("KEY_STORE_PASSWORD"), "password");
String algorithm = StringUtils.defaultString(System.getProperty("KEY_STORE_ALGORITHM"), "SunX509");
I need to load those values dynamically. For that purpose, I need to set those values as environment variables(custom) in Tomcat. I am running the application using the Tomcat plugin. I tried with setenv.bat file concept. I added the following line into it.
set KEY_STORE_PASSWORD=password
but it I am not getting it my logs. I used another method
set JAVA_OPTS=-DKEY_STORE_PASSWORD=password
I added this line, tried, yet nothing...except null get printed in the console. I don't know what is JAVA_OPTS, I didn't add any System environment variables for Tomcat. Should I add them first?? What are those variables we need to add as environment variables for Tomcat?? Is JAVA_OPTS one of them??
Can I create custom environment variables without creating them??
String pswd1=System.getProperty("KEY_STORE_PASSWORD");
logger.info("pswd1 from tomcat"+ pswd1);
These are the printing statements I am using.
This was definitely a rough one, because all of the approved answers here in the stack over flow failed in my case. I tried with both Setenv.bat & Catalina.bat files I even tried creating a configuration file in the CATALINA_HOME/conf folder of the tomcat, called variables.conf declaring all the variables, both key and values,I need to access as an environment variable.
Pass user defined environment variable to tomcat
All of these methods in the above link failed and finally I tried this one using JVM Settings of the Tomcat
Open Window -->> Preferences -->> Tomcat -->> JVM Settings
Here in the Append to JVM Parameters, add your variables which you need to work as environment variables(which can be accessed in the entire project using System.getProperty() ).
An example is shown in the picture below:
Here my variables are :
KEY_STORE_PASSWORD=
KEY_STORE_ALGORITHM=
KEY_STORE=
KEY_STORE_PROTOCOL=
which can be written to JVM Settings as -D followed by variable [equals to] variable value.
Eg: -DKEY_STORE_PASSWORD=password
If you have any queries, please do ask.
#Bruce Wayne
Thanks for this answer! I could not find an example of how to do this anywhere!
ADDITIONAL HELPFUL INFO for debugging with VSCode and the Tomcat for Java Extension
When debugging with Tomcat by right clicking on the tools folder and selecting Debug with Tomcat Server launch.json is NOT used. So trying to set the environment variables there does not work.
Right click on the Tomcat server and select "Customize JVM Options"
Then set your variables as indicated above with the -D option in the jvm.options file that appears and save
so
-DVARIABLENAME=VALUE

Tomcat as Windows service how to pass a Java property [duplicate]

This question already has answers here:
Passing JVM arguments to Tomcat when running as a service?
(2 answers)
Closed 3 years ago.
We currently run the tomcat 8.5 as windows service. How is it possible to pass over a java property i.e. via -D parameter to the startup of the service?
do we can pass over a parameter through the service properties dialogue "startup parameters" of the windows operating system (Service Manager). If yes what would be the syntax?
do we need to hard code that property somewhere in the startup bat files of tomcat, if yes which ones?
other possibilities i.e. system variable like CATALINA OPTS?
Thank you and regards
Mark
First sure that you had ran Service.bat install then please run Tomcat7w (or Tomcat8w, Tomcat9w if you use another Tomcat version).
Select and add Java Properties in Java Options.
If you install service with another name, example service.bat install TomcatApp then please rename Tomcat7w to before open.
This is from the Environment Variable Prerequisites section of the catalina.bat script in Tomcat 8.5:
rem JAVA_OPTS (Optional) Java runtime options used when any command
rem is executed.
rem Include here and not in CATALINA_OPTS all options, that
rem should be used by Tomcat and also by the stop process,
rem the version command etc.
rem Most options should go into CATALINA_OPTS.
You should be able to set the JAVA_OPTS environment variable with your custom -D parameters.

Create a WebLogic development domain with custom memory

I've been trying to automate the creation of our development environment by combining batch files and WLST, but I am struggling to change the memory WebLogic server will start with.
Currently we are manually changing the memory settings in the <DOMAIN_HOME>/bin/setDomainEnv.cmd script, but this is a workaround. It should be possible to to do it automatically without much effort.
Setting the Domain
The script that sets the Domain in pretty simple:
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
set MW_HOME=C:\dev\wls1036_dev
set DOMAIN_HOME=C:\dev\domain
cd %MW_HOME%
call configure.cmd
mkdir %DOMAIN_HOME%
cd %DOMAIN_HOME%
%JAVA_HOME%\bin\java.exe -Xmx1024m -XX:MaxPermSize=256m -Dweblogic.management.username=weblogic -Dweblogic.management.password=welcome1 weblogic.Server
I've tried to use some variables in this script such as MEM_ARGS, JAVA_OPTIONS, but none of these are forwarded to the final configuration of the domain, later it always starts with 512 heap, and 128 perm, which are not enough.
WLST memory start args
We are using Eclipse, and it does call the startWebLogic.cmd as start script. It is the standard configuration.
I tried to use the following WLST script. It does set the server start arguments, but WebLogic is not using those properties and loads not enough memory.
edit()
startEdit()
cd('/Servers/myserver/ServerStart/myserver')
cmo.setArguments('-Xmx1024m -XX:MaxPermSize=256m')
activate()
Any ideas?
You can use the trick for getting ServerStart arguments:
Write simple offline WLST-script to get arguments from config.xml:
getArguments.py
import sys
readDomain(sys.argv[1])
cd('Server/%s/ServerStart/NO_NAME_0' % sys.argv[2])
argsFile = open('arguments.txt', 'w')
print >>argsFile, cmo.arguments
Add this script to startWeblogic.cmd like:
startWebLogic.cmd
...
set DOMAIN_HOME=%~dp0
path\to\wlst.cmd getArguments.py %DOMAIN_HOME% admin_server_name
set /p EXTRA_JAVA_PROPERTIES=<arguments.txt
call "%DOMAIN_HOME%\bin\startWebLogic.cmd" %*
There is no easy way of setting values when executing WebLogic from Eclipse. It'll call the batch script and, at least at current version, does not allow to send dynamic parameters.
We solved it making the setDomainEnv.cmd file part of our versioned configuration:
Copy the setDomainEnv.cmd file to your version control configuration.
Edit whatever you want (memory, etc)
Copy the file like copy custom\setDomainEnv.cmd %DOMAIN_HOME%\bin /y when running your development environment configuration script.
Now every time you configure your development environment memory values will be ready without manual intervention.
You have to reedit your stuff when updating WebLogic, so you don't end up with an outdated component.

Tomcat: External JVM Configuration

I wouldn't ask this here unless I had exhausted the Tomcat reference guide and online searching had produced nadda.
Tomcat has a bin/catalina.sh file where you can specify JVM options to start up with. For purposes outside the scope of this question, I'm wondering if Tomcat supports external configuration of these Java options outside catalina.sh.
Thus, I'd be able to write some file, say, new-jvm-opts.xml, and restart Tomcat, and have it use the options set up in this file (overriding any specified in catalina.sh).
The catalina.sh script allows for Environment Variables to be already set on its startup. If you want to get JVM Opts in there, I think you just set them into whatever Environment your tomcat is starting in. Its documented in catalina.sh as "Environment Variable Prerequisites".
The ones I think you are interested in are either
# JAVA_OPTS (Optional) Java runtime options used when the "start",
# "stop", or "run" command is executed.
#
or
# CATALINA_OPTS (Optional) Java runtime options used when the "start",
# or "run" command is executed.
#
Use setenv.sh setenv.bat it's in the documentation - create a file called setenv.[sh/bat] depending on whether your running windows or unix. Add your vm args
set CATALINA_OPTS=-Xms512m -Xmx1024m
The setenv file is picked up on startup and applies your vm args, whilst avoiding editing your catalina.[sh/bat]

Passing JVM arguments to Tomcat when running as a service?

I need to pass a couple of JVM arguments to the JVM which Tomcat is running in so that my application can pick them up.
I want to follow the process outlined in this article to pick up environment variables.
How would I go about doing this?
UPDATE
Sorry This is running under windows (7 on my Dev machine 2003 on client server)
Windows:
In your Tomcat /bin folder, you should have a tomcat5w.exe admin app (or in later versions tomcat6w.ex, tomcat8w.exe, etc). Go to the Java tab and add the args in the "Java Options:" box.
Note that when you add new args, you need to add them as NEW LINES in that box (above or below any others there), not as additional arguments IN FRONT or BACK of values on one of the existing lines.
Linux / UNIX:
In *nix, changes to the setenv.sh file should be picked up:
export JAVA_OPTS=-server -Xms2g -Xmx4g -XX:PermSize=64m -XX:MaxPermSize=256m $JAVA_OPTS
Don't touch catalina.sh or other files in bin
You need to edit the Windows service. There are three ways to do this:
Start Tomcat5w with //MS//ServiceName to get an icon in the system tray which gives you a quick access to the configuration of the service.
Open the service manager in the "Control Panel". There is an entry for Tomcat.
In the editor, there is a tab where you can add additional JVM parameters.
The third way (which I prefer) is to write a script which edits the config for you. This way, you can save the config somewhere for backup. See the docs how to do that (Hint: use tomcat5 //US//...)

Categories