Saturday, October 20, 2007

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.

12 comments:

Mr. Blue Coat said...
This post has been removed by the author.
Anonymous said...

Does it work for Glassfish V3 ??
Regards

Byron Nevins said...

The V2 procedure is not supported in V3 and will not work.

Here is a blog that describes how to create a service in V3.

http://blogs.sun.com/foo/entry/platform_services_available_in_v3

Pranab Sharma said...

Dear Ryan,
Good utility. I tried and its very simple. Thanks for your effort. It was very helpful.
Regards,
Pranab S.

Gavin said...

Thanks for a great app Ryan. I do have one issue though, have first service (domain1 for example) installed and working, however, even when using -m domain2 for a second domain (required for different IP) it still defaults the install to domain1. Any ideas?

Gavin said...

*(required for different port), sorry, messing up the thread now :-/

Ryan de Laplante said...

@Gavin: Thank you, I'm glad you find it useful. In the services window, right click the GlassFish service and select properties. Examine the asadmin command it runs. Figure out how to make it work from the command prompt (asadmin start-domain ...) then let me know if there is a bug when building the startup command for Windows services. You can also look at the source code linked to from this blog. It is a very tiny single class program.

Anonymous said...

HI

i have only glass fish jar what will steps to execute it?
I am unable to install it.

Thanks and Regards
Ankur Thakur

Ryan de Laplante said...

You have "only glassfish jar", what does that mean? Are you using GlassFish V3? The tool I write is designed for GlassFish V2. If you are using V3, it comes with built-in support for creating Windows services:

http://blogs.sun.com/foo/entry/platform_services_available_in_v3

grzeniek said...

Hi, it looks like it doesn't work under windows 2008 server :-(

output:
glassfishsvc v1.0 (Aug 19 2007)

Service name : GlassfishAppServer
Glassfish installation path : C:\glassfish
Glassfish domain : domain1
Glassfish admin username : admin

Installing service...
Done.
but it does not create service :-(

Any ideas

Ryan de Laplante said...

That is strange because I just used it on a Windows 2008 server the other day (for the first time) and it worked. Make sure you put GlassFishSvc.jar in the GlassFish root directory (C:\glassfish\ in your case?). Make sure you cd\glassfish before running the tool. Also make sure you have rights to create a Windows service (Administrators only?).

Someone mentioned that this tool doesn't work for GlassFish V3, but also might no longer be needed for V3.

grzeniek said...

You right, probably I just forgot to run GlassFishSvc.jar tool with administrator rights (I run this tool as Administrator, but I didn't open console with 'Run as administrator').

I will try to use it again next time I will install glassfish, but for now I just used sc.exe to create windows service.

thanks

Post a Comment