Question

I have been trying to get a Windows service running from my JAR file. WinRun4j seems to be able to do the job, but I can't get it to work. I am especially finding it quite difficult to debug. I tried several methods for logging (writing to a .txt file, WinRun4j's EventLog class) but I can't seem to generate any output.

The service installs fine (eventually..) and I can start it. It should start a Jetty server that generates an XML file that can be reached over HTTP. The app works for a stand-alone version, just not for the service. The service is started, but as soon as I call the URL it stops without generating an error.

This is my Service class:

package com.some.package;

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

/**
* A basic service.
*/
public class StockService extends AbstractService {
   private StockServer srv;

    public int serviceMain(String[] args) throws ServiceException {
    while (!shutdown) {
        try {
        Thread.sleep(5000);
        } catch(InterruptedException e) {

        }
        if(srv == null) {
    try {
            srv = new StockServer();
        srv.start();
    } catch (Exception e) {

        }
        }
    }

    return 0;
    }
}

I found out that the service didn't want to start if I started the Jetty server from the serviceMain class. I had to start a new thread. So StockServer extends Thread:

public class StockServer extends Thread {

    private Server server;

    public void run() {

        if (server == null) {
        try {
        server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/example");
        StockServlet stockServlet = new StockServlet();

        context.addServlet(new ServletHolder(stockServlet), "/stock/*");

        server.setHandler(context);
    server.setStopAtShutdown(true);
    server.start();
        server.join();
        } catch (Exception e) {
        System.out.println(e.getMessage());
        }
    }
    }
}

Since it runs perfectly fine as Java application I just don't know how to get this thing debugged. I hope one of you can point me in the right direction :).

No correct solution

OTHER TIPS

I ended up using the Java Service Wrapper (JSW). This seemed a lot more complex but ended up to be quite easy. It also provides logging by default so I could easily fix the errors. The JSW had problems finding the correct JDK, since JSW is 32bit and I installed JDK1.7 64 bit (and 1.6 32bit). Installing JDK1.7 32bit fixed it. That might have been the problem with WinRun4j as well, but that is something I will never know :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top