Windows‎ > ‎

Window Service

Create a Windows Service:
sc create NewService binpath= "ntsd -d c:\nt\system32\NewServ.exe"
Delete a Windows Service:
sc delete <service name>



create a java service, named AService, using Apache Commons Daemon (prunsrv.exe)

prunsrv.exe //IS//AService --Install=C:\aservice\bin\prunsrv.exe --Description="A Service" --Jvm=C:\opt\jdk\jre\bin\server\jvm.dll --Classpath="C:\aservice\lib\commons-codec-1.3.jar;r;c:\aservice\bin" --StartMode=jvm --StartClass=info.compute.example.aservice --StartMethod=windowsService --StartParams=start --StopMode=jvm --StopClass=info.compute.example.aservice --StopMethod=windowsService --StopParams=stop --LogPath=C:\apserver\logs --StdOutput=auto --StdError=auto

to delete:

prunsrv.exe //DS//AService

Note that your start class has to have the following function:

    public static void windowsService(String args[]) {
        if(args[0].equals("start"))
            main(null);
        else if(args[0].equals("stop")) {
            logger.info("AService stopping - shutdown hook called from Windows Service");
           
            Runnable r = new Runnable() {
                public void run() {shutdownService();}
                };
            Thread t = new Thread(r);
            t.setName("shutdown");
            t.start();
        }
    }

    public static void shutdownService() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.exit(0);
    }






Comments