Creating a Windows service for Glassfish V2

In the past I have chosen to use the commercial version of Glassfish (Sun Java Application Server 9.0), because it comes with a graphical installer and our customers have the option to buy support for it. Other than those two differences, they are exactly the same product. When used on Windows, the SJAS graphical installer has a checkbox on one of the screens to create a Windows service. Glassfish does not come with a graphical installer, and requires a full JDK for installation. It is still simple to install, but lacks an easy way to create a Windows service.

The final release date for Glassfish V2 (and also Sun Java Application Server 9.1) is September 17 2007. I've been using Glassfish V2 lately because of the features, performance, and I wanted to make sure that my apps run on it. The instructions to create a Windows service look like this:

C:\windows\system32\sc.exe create domain1 binPath= "C:\Sun\AppServer\lib\appservService.exe \"C:\Sun\AppServer\bin\asadmin.bat start-domain --user admin --passwordfile C:\Sun\AppServer\password.txt domain1\" \"C:\Sun\AppServer\bin\asadmin.bat stop-domain domain1\"" start= auto DisplayName= "SunJavaSystemAppServer DOMAIN1"

It may be simple enough to change the paths to match your own to get this to work, unless there are spaces such as C:\Program Files\Sun\GlassfishV2\. Try figuring out the correct sequences of escaping the escape characters etc. After over an hour I had it almost working, but still failed. When I asked Sun for help they provided me with the few lines of source code from the Sun Java Application Platform SDK graphical installer that builds the properly escaped command line. I turned this into a command line utility that is very simple to use.

The simplest way to use the utility is to copy GlassfishSvc.jar to 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\GlassfishV2RC1 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 ijws dot com>

Download links: GlassfishSvc.jar, GlassfishSvc.java

I hope that there are Glassfish users who find this utility helpful, and that the Glassfish project either links to this utility from their installation manual or adopts it as part of the project. There is no license, do what you want with it.

Sep 6 2007 UPDATE: I have since learned about a couple of important issues related to running Glassfish as a Windows service.

  • 1) After you log out of Windows the java.exe process will terminate. Meanwhile, the appservService.exe Windows service wrapper that comes with Glassfish will continue to run, and Windows will think that the service is still running. The JVM has a built in feature that shuts itself down when it receives an event from Windows indicating that a user is logging out. You can disable this "feature" by adding the -Xrs JVM option to Glassfish's domain.xml. In Glassfish's domains\domain1\config\ folder you will find domain.xml. Open the file and search for jvm-options. You'll see that each JVM option is on it's own line inside of an XML element. Add a new line using the same syntax, but with the -Xrs option. Save then restart the Glassfish Windows service for the change to take effect.
  • 2) If you are running the cluster profile, Glassfish will ask for the admin password every time the service starts. This was a bit confusing to me because it only asked for a password on some computers and not others. Later, it asked for a password on all computers. You need to create a password file in a specific format and tell the Windows service to use it. Luckily the GlassfishSvc.jar tool that I wrote has a -p option to do exactly that. The -p option creates a file called password.txt in Glassfish's root directory. You should use NTFS security options to ensure that only the "Local system account" (the service) can read/write this file.

Comments (10)

Comments:

Ryan,
Nice to see that you have converted the code to an utility. Hope it helps many.

-sathyan

Posted by sathyan catari on August 20, 2007 at 02:35 AM EDT #

hi there,

thanks for the tool.

sc.exe is not available on windows 2000 systems. How would one do it in that case ?

BR,
~A

Posted by anjan bacchu on August 21, 2007 at 11:43 PM EDT #

sc.exe is part of the Windows 2000 Resource Kit:
http://www.activexperts.com/activmonitor/windowsmanagement/reskit2000/

You can download sc.exe from here:
ftp://ftp.microsoft.com/reskit/win2000/sc.zip

Posted by Ryan de Laplante on August 22, 2007 at 12:08 AM EDT #

Very useful. --drg

Posted by Dennis Gesker on August 22, 2007 at 10:09 AM EDT #

cool... and works immediately... thank you.

can you provide the same for Derby (java-DB) ?

greets
markus

Posted by markus on August 27, 2007 at 07:43 AM EDT #

Thanks. I've never used java-db so don't know anything about it. Feel free to modify the source to create a similar program for java-DB. If you email it to me, I'll put a link to it on this page.

Posted by Ryan de Laplante on August 27, 2007 at 08:37 AM EDT #

[Trackback] Se por algum motivo você realmente precisar instalar um servidor de produção em uma maquina com Windows, ou se isto for necessário para o seu sistema (precisa ganhar o selo de compatibilidade da MS), ou simplesmente por que você gosta de botar o d...

Posted by Blog do Urubatan on August 28, 2007 at 03:22 PM EDT #

I just used your jar file to create a service for the AT&T wireless billing simulator at Ericsson. Great work!

Posted by Britton LaRoche on September 04, 2007 at 08:59 PM EDT #

[Trackback] Contents Overview The Java API for XML Web Services (JAXWS) is a Java programming language API for creating web services. It is part of the Java EE platform from Sun Microsystems. Like the other Java EE APIs, JAXWS uses annotations,...

Posted by Confluence: IRAD SOA OG on September 25, 2007 at 06:31 PM EDT #

Great! Thanks mate

Posted by James on October 11, 2007 at 03:00 AM EDT #

Post a Comment:
Comments are closed for this entry.