Pages

Wednesday, June 1, 2011

Creating a Windows Service for Glassfish V2.

I can think of quite a few reasons why you want to create a service for your Glassfish domains. With the Glassfish V3 release, it is now very simple to create a service at the install time and the installer will do the work for you. The V2 however is a different story.

That said, it is still very easy to create a service for Glassfish V2 domains. The easiest way is to use the sc.exe windows utility to create and remove services. You can find more details about this command here.

Usage is fairly simple. Open the command prompt as administrator and type in the following command:
sc create [service name] binPath= "[Path to appservService.exe] \"[path to asadmin.bat] start-domain [domain name]\" \"[path to asadmin.bat] stop-domain [domain name]\"" start= auto DisplayName= "[display name]"

Just replace everything within the [] with your details. Noticeable things here are the [service name] and [display name]. The service name will be the ID of your service whereas the display name is the name you will see on the Services panel under administrative tools.
For example here is the sample command with full path names according to the installation on my machine:
sc create GFService binPath= "C:\software\glassfish\lib\appservService.exe \"C:\software\glassfish\bin\asadmin.bat start-domain domain1\" \"C:\software\glassfish\bin\asadmin.bat stop-domain domain1\"" start= auto DisplayName= "GFService"

Simply replace the installation paths in this command and it will create the service for you. You can see the service in the Services panel in administrative tools as GFService. Try using that service to start-stop-restart your domain and see how it works.

If you find any issues, remove the service and create this again after double checking the syntax. You can remove the service using this command:
sc delete [service name]
For me it would be:
sc delete GFService

Note here that I am using the [service name] in this command. If your display name is different then you need to use the service name. Unfortunately the Services panel only show the Display Name and you need to open the properties of that service to find out the service name. That is why just to keep things simple, I am using the same display name as the service name.

Before you use the sc command make sure you double checked all of the following or it will not work.

  • Double check if you have sc.exe in C:\Windows\System32
  • Make sure you have opened the command prompt as administrator.
  • Note the space between the parameter and value (start= auto NOT start=auto) without the space it will not work. 
  • Make sure the path to appservService.exe and asadmin.bat are correct.
  • Make sure you have all the \ s and " in the right place and with the correct spacing. 
  • This is one single command on one line, you have to type it in all at one line before you hit enter. 


If you find it difficult and for whatever reason this does not work for you, don't despair. You can also use a very simple utility created by Ryan de Laplante that will install Glassfish V2 on any windows machine.

He has give complete description on how to do it in this post that you can read for any further information. Here I am trying to further simplify how to do that.

Download the .jar file.
Place the Glassfish-as-Service.jar in the glassfish home folder

Run the following command as admin.

java -jar Glassfish-as-Service.jar -i -n [ServiceName] -d ["Glassfish home path"] -m [domain]

Example:
java -jar Glassfish-as-Service.jar -i -n Glassfish -d "c:\\software\\glassfish" -m quote

glassfishsvc v1.0 (Aug 14 2007)

Service name                : Glassfish
Glassfish installation path : c:\software\glassfish
Glassfish domain            : quote
Glassfish admin username    : admin

Installing service...
Done.

This commands can by successful Only when running command prompt as admin


Ryan have provided both the source and the jar for us. A big thank you to Ryan. He did not imposed any licensing restrictions on this work.
Download links: GlassfishSvc.jar, GlassfishSvc.java

That's it. Now you can go to the Services panel and you can start and stop the Glassfish V2 from Services panel of Windows.

For further details about this simple utility Ryan has created this Oracle wiki page for us.