How do I run GlassFish as a Windows service?

Written by Ryan de Laplante

Glassfish comes with a Windows executable called appservService.exe which communicates with the Windows service controller. It works similarly to programs such as Java Service Wrapper. The Windows service you create for Glassfish starts appservService.exe with several parameters that tell it which asadmin commands to execute when starting and stopping.

Glassfish does not come with a tool to easily create a Windows service for appservService.exe, however the installer for Sun Java Application Server 9.x has an "Install as a Windows service" checkbox. If you want to create a Windows service for Glassfish, the instructions show a rather large command to run from the command prompt. If your Glassfish installation path contains spaces such as C:\Program Files\Sun\GlassfishV2\, then finding the correct way to escape the escape characters becomes complicated rather quickly.

GlassfishSvc.jar

Ryan de Laplante has contributed a simple command line tool that makes the creation of a Windows service effortless. Download GlassfishSvc.jar (source) and copy it into your Glassfish installation path (such as C:\Program Files\Sun\GlassfishV2), then run the following command from the command line:

C:\Program Files\Sun\GlassfishV2>java -jar GlassfishSvc.jar -i 

glassfishsvc v1.0 (Aug 19 2007) 

Service name : GlassfishAppServer 
Glassfish installation path : C:\Program Files\Sun\GlassfishV2
Glassfish domain : domain1 
Glassfish admin username : admin 

Installing service... Done.

The -i argument will install the service with default options. A complete listing of the command line arguments follows:

glassfishsvc v1.0 (Aug 19 2007)

DESCRIPTION:
    Installs and uninstalls a Windows service for Glassfish

USAGE:
    java -jar glassfishsvc.jar [-i | -u] [OPTIONS]

    -i        Installs a Windows service for Glassfish.
    -u        Uninstalls a Windows service for Glassfish.
    -n name   Name for the Windows service. Use double quotes around names
              that contain spaces.  Defaults to GlassfishAppServer.
    -d path   Directory where Glassfish is installed. Use double
              quotes around paths with spaces, and escape back slashes.
              Defaults to current directory.
    -m domain Name of the Glassfish domain to start and stop. Defaults to
              domain1.
    -a user   Glassfish admin user name.  Defaults to admin.
    -p pwd    Glassfish admin password.  A password.txt file will be created
              in the Glassfish install directory containing the password in
              plain text, and the Windows service will be configured to read
              from it. This is usually not necessary. If no password is passed
              in, the password.txt file will not be created.

EXAMPLES:
    java -jar glassfishsvc.jar -i
    java -jar glassfishsvc.jar -i -p adminadmin
    java -jar glassfishsvc.jar -i -n MyServiceName -d "C:\\Program Files\\Sun\\Glassfish" -m myDomain -a admin5 -p secretpwd
    java -jar glassfishsvc.jar -u
    java -jar glassfishsvc.jar -u -n MyServiceName

AUTHOR:
    Ryan de Laplante <ryan at gmail dot com>

Cluster Profile

If you installed the Glassfish cluster profile then Glassfish will ask for the admin password as the service starts. To get around this use the -p option when installing the service. A password.txt file will be created in Glassfish's root directory containing the password in plain text. The Windows service will be configured to use this file so that it can start unattended. You should make sure to configure appropriate NTFS security on this file after it is created so that unauthorized users do not have read or write access to it.

Windows User Log-out

By default the JVM catches signals from Windows that indicate the OS is shutting down, or a user is logging out, and shuts itself down cleanly. This means that the Glassfish service will be shut down any time a user logs out of Windows. This defeats the purpose of having a Windows service so we need to turn off this JVM feature by using the -Xrs JVM option.

Open the <GF_HOME>\domains\domain1\config\domain.xml file and locate the section that has a lot of <jvm-options>some JVM option</jvm-options> lines. Add the following line in that section:

<jvm-options>-Xrs</jvm-options>

Save and close domain.xml. If the Glassfish service is already running, restart it for the change to take effect.

Note: There have been reports that adding the -Xrs jvm-option to domain.xml does not solve this problem on some Windows 2003 Server installations. The reported solution is to add the jvm option to <GF_HOME>\lib\processLauncher.xml:

<process name="as9-server">
   ...
   <sysproperty key="-Xrs"/>
   ...

Resources

Ryan de Laplante's blog entry
Sun Java System Application Server Platform Edition 9 Administration Guide - Chapter 20
The Aquarium - Windows Services Support in SJS AS (and GlassFish)
java.net forum post on processLauncher.xml