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.


Solution
  • 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)