3

I've been reading the documentation but I can't start and stop de service.

My .ini file is:

main.class=test.TestService
service.class=test.TestService
service.id=StreamServer
service.name=StreamServer
service.description=Servidor que proporciona una comunicación con streams.
service.controls=stop   
classpath.1=*.jar

The TestService class is:

package test;

public class TestService{
    private static TestServer server;

    public static void main (String[] args){
        if (args.length == 1){
            if (args[0].equals ("start")){
                if (server == null){
                    server = new TestServer (5000);
                    server.start ();
                }
            }else if (args[0].equals ("stop")){
                if (server != null){
                    server.stop ();
                    server = null;
                }
            }
        }
    }
}

I have to modify this class but I don't know how.

Thanks.

Gabriel Llamas
  • 17,220
  • 24
  • 84
  • 105
  • 1
    What documentation are you reading? You can't create a native windows service directly in Java. Check [this discussion](http://stackoverflow.com/questions/68113/how-to-create-a-windows-service-from-java-app) for some solutions. – jdigital Apr 22 '11 at 20:16
  • 1
    The title says winrun4j. http://winrun4j.sourceforge.net/ – Gabriel Llamas Apr 22 '11 at 20:46

1 Answers1

5

Take a look at the sample service from the front page of the winrun4j site:

package org.boris.winrun4j.test;

import org.boris.winrun4j.AbstractService;
import org.boris.winrun4j.EventLog;
import org.boris.winrun4j.ServiceException;

/**
 * A basic service.
 */
public class ServiceTest extends AbstractService
{
    public int serviceMain(String[] args) throws ServiceException {
        int count = 0;
        while (!shutdown) {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
            }

            if (++count % 10 == 0)
                EventLog.report("WinRun4J Test Service", EventLog.INFORMATION, "Ping");
        }

        return 0;
    }
}

The serviceMain method is invoked when your service is started. You should not return from this method until your service is ready to shutdown. Also check the "shutdown" flag - this will be set to true when you click on Stop in the service control panel (or when your service needs to be stopped).

Peter Smith
  • 713
  • 6
  • 7