Friday, March 16, 2012

Running GlassFish as a service on Windows (Error 1067)

I've been trying to run GlassFish as a service on Windows:

asadmin.bat create-service

Unfortunately, that task didn't show up easy due to one little detail: GlassFish was installed on the famous Program Files folder. Other causes may just need a restart of Windows.

When starting the service, it says: Error 1067: The process terminated unexpectedly. And it really terminates.

Because there is a space character between Program and Files, the path to GlassFish also has spaces. The way GlassFish is called when requested via the service start makes it impossible to resolve the path correctly if there are spaces and other special characters, on Windows.

First of all, make sure you restarted your Windows after installing GlassFish as a service. I also had this problem when not using spaces in the path. After restarting the system, everything got fixed by itself.

Update: The following content, in a gray font, is composed of some workarounds for solving this problem. These workarounds are not required anymore because Byron Nevins just found out the right fix for this problem. Skip to the Solution.

I will not present the perfect solution, but some possible workarounds for this, although I encourage you to share your experiences below in the comments:

1.Just move your whole GlassFish installation to a directory with no spaces behind. It'll just work, no issues.

2.If you really want GlassFish to live in your old directory, you can create a symbolic link to that directory and put it in a legal path.
mklink /d C:\glassfish-3.1.2 C:\"Program Files"\glassfish-3.1.2
Now, create the service using the usual method and still at the old path:
asadmin.bat create-service
And edit the file C:\Program Files\glassfish-3.1.2\glassfish\domains\domain1\bin\domain1Service.xml (assuming base path is C:\Program Files):

  • Remove every occurrence of: Program Files\\ or Program Files/, thus making the service executable point to your new symbolic link.

3.Create a service by hand, using sc (don't forget the space before the equal signs):

sc create MyGlassFishServiceName binPath= "C:\Program Files\glassfish-3.1.2\bin\asadmin.bat start-domain domain1" start= auto
This will create a service based on the usual GlassFish administration script, asadmin, so it's not a clean way and some errors may be thrown when manually starting the service. Also, it cannot be stopped. If you just want GlassFish to run as a service at startup, then this probably fits you enough. Update: Thanks to the fellow follower Micael Capitão that pointed out some typos in my command.

The Perfect Way.By editing the file I mentioned above, domain1Service.xml, we should be able to escape the spaces from from the path (usual escaping doesn't seem to work). Also, that could come as default in future versions of the splendid Oracle GlassFish (and the open source edition too, for that matter). Oracle already knows of the problem, like one of the following links reveals. If anybody knows/discovers how this can be achieved, please post in the comments below.

  • After creating the service, edit the file:
     C:\Program Files\glassfish-3.1.2\glassfish\domains\domain1\bin\domain1Service.xml
    (assuming glassfish is installed at C:\Program Files\glassfish-3.1.2)

  • Find the line:
    <startargument>C:\\Program Files\\glassfish-3.1.2\\glassfish\\domains</startargument>

  • Add double quotes around the path, like this:
    <startargument>"C:\\Program Files\\glassfish-3.1.2\\glassfish\\domains"</startargument>

  • Do the same for the line:
    <stopargument>C:\\app\\big glass\\glassfish\\glassfish\\domains</stopargument>

Some useful links:
How do I run GlassFish as a Windows service? (Oracle)
How to create a Windows service by using Sc.exe (Microsoft)


  1. Hi,

    It will start and stop for you like a normal service. You need to use the "--verbose" option for start-domain. Try it at a command-line. What happens is the asadmin JVM stays alive, fires up the DAS and hangs around until it dies.

    Now if you kill the asadmin JVM -- the server will definitely go down with it.

    I'll look into the space-in-path problem. Thanks for finding this problem.

  2. Here is the easiest solution
    Simply surround the path in the xml file with double quotes. E.g.

    "D:\\a b\\glassfish\\domains"

    I tested it and it works. I'll put the fix in the trunk of GF code today. Thanks!

  3. Using the --verbose option makes no effect on the start of the service here. I still get the error 1053 (The service did not respond to the start or...). GlassFish actually starts after that, but because the service didn't officially start, there's no way of telling it to stop...

    The double quotes solution really works! I had only used double quotes in the section and not in the ones. I'm gonna update this post with the perfect solution right away!

    Many thanks! I'm looking forward for the first GlassFish version with your fix.

  4. Corrections:

    (1) Use the --watchdog option instead of --verbose so you don't get all log messages duplicated.

    (2) There is a bug in winsw (domain1Service.exe). The bug is fixed in a newer version of winsw. I have permanently added the following work-around to the GF trunk codebase. I'll also try to get it into the first patch of 3.1.2. The work-around fixes the problem completely:

    * Put double quotes around all paths in and
    * DO NOT double-quote the or (winsw is already doing it for you!)

    1. It hacked-off the tags -->

      it should say to double quote all paths in the html tags startargument and stopargument

    2. Sorry. The second part of the work-around is to leave alone the logpath and executable paths.
      DO NOT quote them or it won't work at all.

    3. So, you say that if I create a service like the following:

      sc create myglassfish binPath= "C:\my folder\glassfish\bin\asadmin.bat start-domain domain1 --watchdog" start= auto

      Then I'll be able to start and stop the service via the windows service manager?

  5. My corrections (the tags got hacked off just like yours):

    where is "I had only used double quotes in the section and not in the ones." should be "I had only used double quotes in the executable section and not in the startargument and stopargument ones".


Feel free to share your thoughts!