How to pass VM arguements to run on tomcat server startup? - java

In our project , in ide we are providing few vm arguements in ecipse run configurations. But when we are deploying the application, we are trying to provide these arguements. But not sure how to provide through tomcat to run on server startup. Can anybody help us here. Thanks in avdance.

The proper way to set VM arguments in Tomcat is to create a file in the Tomcat bin directory called "setenv.bat" (or "setenv.sh" if you're in Linux or Mac). In that file, you can set JVM options by setting the JAVA_OPTS variable, like for instance:
set JAVA_OPTS="-Xmx2G -Xms128M -Dvariable=2"
https://docs.oracle.com/cd/E40520_01/integrator.311/integrator_install/src/cli_ldi_server_config.html

Related

How to read Ubuntu environment variables into SpringBoot Program?

Here's what I do in SpringBoot on Windows to read an environment variable (location of log folder).
In Windows Server, I set a System environment variable for "LOG_HOME" with the value with the directory that SpringBoot should use to write logs.
In SpringBoot's application.properties, I have:
logging.file.name= ${LOG_HOME}/ws.log
Works great!
But in Ubuntu Linux 20.04, the same approach doesn't work for me at all.
When the WAR file tries to deploy on Ubuntu 20.04 using this similar technique:
(in .bashrc): export LOG_HOME = /home/ubuntu/logs
reboot (to reload the environment for sure)
I get this error in the Tomcat log when trying to deploy the WAR file:
java.lang.IllegalArgumentException: Could not resolve placeholder 'LOG_HOME' in value "${LOG_HOME}/ws.log"
So, it seems that Spring doesn't see the environment variable set in Ubuntu.
I wrote a simple Java program just to check the value of the environment variables and they were all created as expected including the LOG_HOME as shown in Linux "printenv".
If possible, I need a technique that will work on Ubuntu without changing the working SpringBoot implementation on Windows Server.
Thanks in advance for suggestions.
Instead of exporting in shell session like
export LOG_HOME = /home/ubuntu/logs
try this as -D VM argument in your starup command
eg:
java -cp=xxx mainclass -DLOG_HOME=/home/ubuntu/log
if you are using tomcat then :
VM args can be added catalina.sh file under CATALINA_OPTS.
For tomcat, add your environment variables to $TOMCAT_HOME/bin/setenv.sh where $TOMCAT_HOME is the directory of your tomcat installation.
The solution for me posted by the extremely helpful satyesht above, was to edit the Catalina.sh file and add the "-D" name-value pair option under CATALINA_OPTS. Thanks to all who posted. :)

JVM arguments while starting tomcat in vscode

I am using "Tomcat for Java" plugin in my Visual Studio Code for starting and stopping tomcat server.
This plugin provide following options
1. "jvm.option" to specify JVM arguments
2. "server.xml" to specify tomcat server.xml configuration.
I want to pass some additional environment variables like below while starting tomcat. I could pass additional JVM arguments in STS/Eclipse by double clicking tomcat.
Can someone help me how I could pass below environment variable in VSCode.
-Dconfig=/x/app/config/production.conf
-DlogbackFile=x/app/config/logback.xml
After adding tomcat to your VS code, you can right click on the tomcat and select 'Customize JVM Options'.
After it is open, you can add the parameters that you add in VM arguements in eclipse in here.

Intellij tomee set tomee.serialization.class.whitelist

How to setup in IntelliJ IDEA VM options.
I need to setup
set JAVA_OPTS="-Dtomee.serialization.class.whitelist="
set CATALINA_OPTS="-Dtomee.serialization.class.blacklist=-"
In run/debug configuration for my local tomee server I pass into VM options
-tomee.serialization.class.whitelist=*
-tomee.serialization.class.blacklist=-
also tried without *.
It still giving me "invalid arguments" error after run/debug the server. How to set it properly?
ps.If I run the server manually everything works correct.
You need to specify the two properties as Java system properties (-D) in the VM options of your run configuration:
-Dtomee.serialization.class.whitelist=*
-Dtomee.serialization.class.blacklist=-
you can set them in idea.exe.vmoptions (should be in bin directory of idea installation) to force it for client side and in conf/system.properties of the tomee instance for the server.

How to set JVM options for Tomcat instance executed from eclipse

I'm developing a Java/Spring/Hibernate application with eclipse which is deployed to tomcat and I'm having serious java.lang.OutOfMemoryError: GC overhead limit exceeded issues.
I want to try to tweak the JVM settings used by the tomcat instance used by eclipse when launching the application.
Do you know how can I set the JVM tomcat parameters from eclipse? +1 if you know which files are affected.
I'm using tomcat 7.0.12, installed from a tar archive on a Linux machine.
I googled for hours but I couldn't find a working solution, TIA.
Under the 'server' view you will see the tomcat server instance listed.
Double click on it. This will open up a page in eclipse having server details. There is a link there called 'Open launch configuration'.
Click on that link and it will give you a dialog window. In that dialog window, under 'arguments' tab under vm arguments you can pass VM args
specify the parameters that are you used by your catalina start up script, found here
tomcat_home/bin
it will use setenv script. Located in catalina_base. You can then add in there :
export JAVA_OPTS="-server -Xmx768m"
or for windows
set JAVA_OPTS=-server -Xmx768m

How to pass a system property to a web application hosted in Tomcat 7 running as a Windows Service?

There are numerous places on the Internet, suggesting that it is easily achieved by any (or all) of the following methods:
through CATALINA_OPTS
through JAVA_OPTS
through TOMCAT_OPTS
by placing the set statements in the setenv.bat file inside the tomcat's bin folder
My problem, is that I have tried all of the above and my web application still does not see my system property!
Here is what I am doing:
Stop tomcat7 service
set CATALINA_OPTS=-Dabc.def=true in the system environment
set JAVA_OPTS=-Dabc.def=true in the system environment
set TOMCAT_OPTS=-Dabc.def=true in the system environment
put all of the above into c:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\setenv.bat (seems totally redundant, but just in case)
Start tomcat7 service
Inspect the environment of the tomcat7 process using the Process Explorer tool - the environment is correct, I do see both CATALINA_OPTS and JAVA_OPTS and TOMCAT_OPTS equal to -Dabc.def=true
run my web app, which is a simple servlet dumping all the system properties to the response stream - abc.def is not amongst them
Please, put me out of my misery and tell me how to do it.
For the Tomcat service, startup settings are stored in the registry under Options key at:
HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat<X>\Parameters\Java
(substitute appropriate Tomcat version where needed).
Edit:
On 64-bit Windows, the registry key is:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat<X>\Parameters\Java
even if Tomcat is running under a 64-bit JVM.
I know this post is almost 9 years old but thought someone might find this useful.
Although #prunge and #mark answers were very accurate and following their logic I was able to add system properties to my tomcat 7 instance running on Windows, there is an easier way.
In the installation directory of Tomcat there is an exe you can run called
%INSTALL_DIRECTORY%\bin\tomcat7w.exe
This opens up a Tomcat properties windows where you can control the service i.e. start and stop tomcat and there is a tab (Java) that allows you to set the Java properties as well
Scroll to the end of that panel under "Java Options" and add your system properties
-Dpropertyname=value
Then navigate back to the General tab and restart tomcat
I have tested this and my grails app now can see my properties. I use the following Groovy code to get the property out
System.properties.getProperty("propertyname")
Adding the system properties in the Windows registry showed up in this window as well so its one and the same thing, just this application seems to me to be slightly more convenient.
Hope this helps someone else

Categories

Resources