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

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.

Related

My Tomcat is not running it just open and close when I click on tomcat7.exe [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I am installing Tomcat 7 in my PC it works in Eclipse but when I try to open in bin/ tomcat7.exe it just open and closes, (I am stopping the server in Eclipse). Also, in Eclipse I'am trying to open admin console like URL:(localhost:1234/admin) it shows 404 error. Can anyone who knows this problem explain a solution?
If you are using Windows, you can follow the following to run tomcat7.exe :
Open your Command prompt (cmd)
Set your JAVA_HOME or JRE_HOME. Eg. set JRE_HOME=C:\Program Files\Java\jre7
Go to the directory of your tomcat. Eg. cd C:\Program Files\apache-tomcat-7.0.35\bin
Run startup.bat file. Eg. startup.
This should start your tomcat server without using Eclipse.
First Set JAVA_HOME and CLASSPath path
For Windows Java Path Settings
Right click on Computer and open Properties.
In Windows Vista or Windows 7, go to Advanced System Settings.
Else go to next step. Go to Advanced Tab and click on Environment
Variables button.
Select CLASSPath under the list of System Variables, and press Edit and add If CLASSPath not available there click New button add
C:\Program Files\java\jre 1.x.x_xx\bin
after a semicolon.
Now click on New' button under system variables and enter
JAVA_HOME as variable name and path to jdk home directory (ex.
C:\Program Files\Java\jdk1.x.x_xx
After Completing Java Path Settings
Go to the directory of your Tomcat. Eg. cd C:\Program
Files\apache-tomcat-7.0.35\bin
Run startup.bat file
To start your application server in Windows :
Open command prompt .(Run as Administrator).
Go to the path (..\apache-tomcat-8.0.27\bin) and run startup.bat
A new command prompt opens up and should stay unclosed to tell your server is in running state.
If command prompt opens and closes automatically, then the reason can be as below.
tomcat is already in started state in Eclipse.
Check the connector port configured in server.xml file and check if that port is free in your local machine. [netstat command]
Change the connector port to some unused port, preferably in 80XX series.
Let me know if you are able to invoke localhost:(the http connector port) you have configured in server.xml through you web browser.
You don't start Tomcat by double clicking the exe, use the batch files instead.
startup.bat starts the server, shutdown.bat stops it again.
And ignore the advise to get the service installer. You don't need those unless you want the server to start on system boot and run in the background. Which you don't want unless you're going to configure a production server, which you aren't here.
The batch files are in the same directory on your computer as the exe.
you have to set JAVA_HOME and JRE_HOME in System environment.while setting the JAVA_HOME and JRE_HOME, you should assure that JRE path that you are setting is corresponding to the JDK whose path you have set in JAVA_HOME. some time what happens that there may be more than one JDK in the system so conflict occurs. better to have only one JDK and corresponding JRE.
This is probably due to a bad setup of an environment variable.
For example, if you setup incorrect JAVA_OPTS, tomcat startup will fail.
You can check your configuration by calling : catalina configtest
Here an example of the output for bad setup of JAVA_OPTS :
Error occurred during initialization of VM Could not reserve enough space for object heap
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
It works for me after updating the jdk path in JAVA_HOME compatible to your tomcat version in user Variables.
the JAVA_HOME and JRE_HOME environment variables set should not have semicolon or ; at the end...I was facing similar problem...removed semi-colon or ; from the end and tomcat server started from cmd screen after running startup command(of tomcat..bin folder).
I checked environment variables and compiler version everything was fine, deleting junk files from work folder worked for me.
Just download the tomcat 32-bit/64-bit Windows Service Installer (pgp, md5) as in this set up is given which will install tomcat on your PC and you can also start and stop tomcat using the tomcat service.
In my experience tomcat7 might not like it if JAVA_PATH or JDK_PATH contain bracket symbols like in "Program Files (x86)". Make sure you format path in your environmental variables correctly or move your JDK to another folder.
In my case, I had some reference to a jar file in catilana.bat, but the jar I had removed from my system. Tomcat started successfully once I removed that reference from catilana.bat
Check your JRE_HOME location in environment variables. if it didn't work then try
1). Open CMD in administration mode.
2). locate to your tomcat directory.
3). run startup.bat --->> It will show your problem.
Had the same problem with Apache Tomcat 9 version.
Solution is very simple.
Apache Tomcat 9 is supported by Java 8 and later versions. So, I had JRE_HOME with JRE7 path, I binded it to new JRE8 and everything works fine.
Different versions of Apache Tomcat are available for different versions of the Servlet and JSP specifications. The mapping between the specifications and the respective Apache Tomcat versions is:

How can I change Session Persistence in Tomcat 7.X?

I am using Tomcat 7.0 and I found a reference of how to change session savings into the database. But I don't get how to change
org.apache.catalina.session.StandardSession.ACTIVITY_CHECK to true
The link that I am referencing is http://www.intelligrape.com/blog/2010/07/21/tomcat-6-session-persistence-through-jdbcstore/
I am using localhost Tomcat 7.X downloaded as zip, MySQL 5.5, STS 3.1(Incuding Eclipse)
Any figuring out?
This is a system property ( http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html ) so to set these you have to specify the -D option. In your case "-Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true".
But where? Well, System Properties are given to the Java process when it starts. In case of Tomcat you can use a environment variable called $JAVA_OPTS which is given to the Java process on startup. So
export JAVA_OPTS="$JAVA_OPTS -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"
and then run Tomcat. For this to persist make sure that the user running catalina.sh has this environment variable always set.
add below line in catalina.bat just after where you found other JAVA OPTS already being set.
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"
e.g.
rem Register custom URL handlers
rem Do this here so custom URL handles (specifically 'war:...') can be used in the security policy
set "JAVA_OPTS=%JAVA_OPTS% -Djava.protocol.handler.pkgs=org.apache.catalina.webresources"
set "JAVA_OPTS=%JAVA_OPTS% -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true"

Pass VM Argument to Apache Tomcat [duplicate]

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

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