Search code examples
javatomcatjvm-argumentstomcat9

Setting JAVA_OPTS while installing Tomcat as windows service


I am running multiple Tomcat instances on the same host and have installed them as windows services. Of course this is with distinct ports for each of the Tomcat instances. Now I am trying to extract the port numbers out of server.xml file and trying to pass them as JVM options, so that the server.xml file looks the same for all the Tomcat instances. Currently the Connector port in my server.xml file for each instance looks like:

Instance 1

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

Instance 2

<Connector port="8180" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

and I am trying to make it look like

<Connector port="${port.http}" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

As mentioned in this answer, I can edit the options manually to add the parameter -Dport.http=8080 or -Dport.http=8180 and it works fine, but what I need is this JVM option to be set when Tomcat is installed as a windows service.

Below is the content of the .bat file I am running to install Tomcat instance1 as windows service (Its the same for instance2 except CATALINA_BASE, port and service name). As you see, I am also trying to set JAVA_OPTS before it is installed as a service, but I don't seem to have any luck with this. I have also tried it with double-quotes like CALL SET "JAVA_OPTS=-Dport.http=8080" and CALL SET JAVA_OPTS="-Dport.http=8080"

CALL SET JAVA_HOME=D:\Java
CALL SET CATALINA_BASE=D:\instance1
CALL SET JAVA_OPTS=-Dport.http=8080
CALL CD %CATALINA_HOME%\bin
service install instance1

Can anyone help please?


Solution

  • Eventually I could resolve it by setting JvmArgs in the batch file that I use to install 'Tomcat' as windows service. The content of my .bat file looks like below:

    CALL SET JAVA_HOME=D:\Java
    CALL SET CATALINA_BASE=D:\instance1
    CALL SET JAVA_OPTS=-Dport.http=8080
    CALL SET JvmArgs=-Dport.http=8080;-Dport.shutdown=8005 // This line did the trick
    CALL CD %CATALINA_HOME%\bin
    service install instance1
    

    I found this out when I was going through the code in service.bat file to check how the JVM options are set and found %JvmArgs% appended at the end as below:

    --JvmOptions "-Dcatalina.home=%CATALINA_HOME%;-Dcatalina.base=%CATALINA_BASE%;-D%ENDORSED_PROP%=%CATALINA_HOME%\endorsed;-Djava.io.tmpdir=%CATALINA_BASE%\temp;-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager;-Djava.util.logging.config.file=%CATALINA_BASE%\conf\logging.properties;%JvmArgs%" ^
    

    Hope this helps someone looking for a similar configuration :)