# Make JOnAS a NT Service

First of all, thanks a lot to Calvin Varney who has provided inputs on the JOnAS users mailing list, which helped us to write this How-to Guide.

The purpose of this document is to describe how to run JOnAS as a NT service, using the open source software jsrvany available on the SourceForge.net web site. The instructions on this site are very easy to follow.

This chapter contains the instructions dedicated to JOnAS.

## Instructions

The following instructions have been tested with the jsrvany version alpha-4 and the JOnAS version 2.4.
In the following, it is supposed that the JOnAS installation directory is `c:\jonas`, and that the JOnAS ORB is RMI.

1. Download jsrvany and unzip the package to your filesystem. Let us suppose for this example, that it is into the directory `c:\jsrvany`.

2. Make `jvm.dll` accessible from your system path. In this example, `c:\Java\jdk1.3\jre\bin\hotspot` has been added to the system path. Note that you have to reboot in order for this to be effective.

3. Create a wrapper class as described at jsrvany site. An example of a such class is given in the following chapter. This example may be used as it is.
   Compile this class with an updated classpath including the JOnAS jar file (`c:\jonas\lib\RMI_jonas.jar`) and the jsrvany jar file (`c:\jsrvany\lib\jsrvany.jar`).
   Create a jar file containing this class and copy it in the `c:\jsrvany\lib` directory. In this example, the name of this jar file is `jonaswrp.jar`.

4. From the `c:\jsrvany\bin` directory, run the installer `instsrv.exe` to install the JOnAS service:

           instsrv -n JOnAS -c JonasWrapperForSrvNT -i c:\jsrvany\bin\jsrvany.exe
       	    -p c:\jsrvany\lib\jonaswrp.jar;c:\jsrvany\lib\jsrvany.jar;c:\jonas\config;c:\jonas\lib\RMI_jonas.jar

   What you see above is one command, i.e. entered on a single line. Do not forget to add any needed jar files (such as the JDBC driver classes if you are using a database). The `-p` option parameter contains at least:

   - `c:\jsrvany\lib\jonaswrp.jar`: jsrvany wrapper class for JOnAS,
   - `c:\jsrvany\lib\jsrvany.jar`: jsrvany package,
   - `c:\jonas\config`: configuration for JOnAS,
   - `c:\jonas\lib\RMI_jonas.jar`: JOnAS package,

   You should see a message like: `JOnAS installed successfully`

5. Run `Regedt32` and edit the key `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControllSet\Services\JOnAS\Parameters\JVMOptions`
   by adding the two following lines:

           -Djava.security.policy=c:\jonas\config\java.policy
           -Dinstall.root=C:\jonas

6. Run the JOnAS service from the Service Control Panel.

7. To deploy Enterprise Java Beans on the JOnAS service, you must build the ejb-jar files as it is described in the Deployer's Guide and copy them in the `%JONAS_ROOT%\ejbjars` directory.
   Then, there are three different ways to deploy these ejb-jar files (let's say for example `app1.jar` and `app2.jar`) on the JOnAS service:

   - Use the JOnAS administration tool `JonasAdmin` or `Jadmin` to load those beans.
     In this case, you have to do it after each restart of the JOnAS service.
   - Or add the line `-Djonas.service.ejb.descriptors=app1.jar,app2.jar` to the `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControllSet\Services\JOnAS\Parameters\JVMOptions` registry entry.
     In this case, you must restart the JOnAS service for it to take effect.
   - Or set the property `jonas.service.ejb.descriptors` to `app1.jar,app2.jar` in the `%JONAS_ROOT%\config\jonas.properties` file.
     In this case, you must restart the JOnAS service for it to take effect.

8. If you need to debug, there is a jsrvany command-line executable that can read the JOnAS service's registry entries and execute the JOnAS service in the same way that jsrvany running as a service would, but which displays any error messages that are normally written to stdout or stderr (and consequently hidden when invoked as a service) on the console where it is launched. To run jsrvany in this debug mode, ensure first that the JOnAS service is stopped, then from the `c:\jsrvany\bin` directory, enter the following command:
   `jsrvany_debug JOnAS`
   Error messages produced should now be visible in the console and should help you to debug the service configuration.

## Example of a jsrvany wrapper class for JOnAS

        import javax.naming.InitialContext;

        import net.sourceforge.jsrvany.ServiceControlException;
        import net.sourceforge.jsrvany.ServiceControlManager;
        import net.sourceforge.jsrvany.SimpleListener;
        import net.sourceforge.jsrvany.StopServiceControlEvent;
        import net.sourceforge.jsrvany.TerminateServiceControlEvent;
        import org.objectweb.jonas.server.Server;

        /**
         * jsrvany wrapper class for JOnAS
         * @author Calvin Varney, JOnAS team
        */
        public class JonasWrapperForSrvNT extends SimpleListener {

            private static ServiceControlManager manager = null;

            public static void main(String[] args) {

                // jsrvany doesn't set the main threads class loader so do so here
                Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
                // get the ServiceControlManager
                manager = ServiceControlManager.getInstance();
                // register this class as a listener
                manager.addServiceControlListener(new JonasWrapperForSrvNT());
                // start JOnAS
                try {
                    Server.getInstance().start();
                } catch (Exception e) {
                    System.err.println("JonasWrapperForSrvNT ERROR when starting JOnAS ("+e+")");
                }

            }

            public void handleServiceControlEvent(StopServiceControlEvent e)
                    throws ServiceControlException {
                stopJonas();
            }

            public void handleServiceControlEvent(TerminateServiceControlEvent e)
    	        throws ServiceControlException {
    	    stopJonas();
            }

            public static void stopJonas() {
    	    // stop jonas from a new thread giving this method a chance to complete
    	    // before the main thread.
    	    Thread stoper = new Thread(){
    	        public void run() {
    		    try {
    		        Server.getInstance().stop();
    		    } catch (Exception e) {
    		        System.err.println("JonasWrapperForSrvNT ERROR when stopping JOnAS ("+e+")");
    		    }
    	        }
    	    };
    	    stoper.start();
            }

        }