Winsw throws an exception “error 1067: unexpected process termination” when converting Java application to Windows Service

Using winsw (GitHub) https://github.com/kohsuke/winsw )It is very convenient to convert Java application into windows service and deploy it on Windows Server.

Detailed operation steps can be searched, the document is relatively rich.

Here’s a problem I had. After I have configured every step according to the documentation, there is no problem with the service installation. Then when I start, the service throws an exception “error 1067: unexpected process termination”

public class App 
{
    public static void main( String[] args ) throws IOException, InterruptedException {
        Logger logger = LogUtil.getLogger();

        logger.info("a");
        logger.severe(new Date().toString());
        
    }
}

The reason is that the main method of Java module is not blocking. When the service is started, winsw will execute the Java – jar command in the configuration file to start a process, and record the process ID of the process, and then there will be a series of operations. The main method here is not blocking. Java jar starts the process, and winsw remembers the process ID. however, when the process information is used later, it is found that the process has terminated, so an exception is thrown.

Solution: the process should be a blocking process, that is, the process cannot end. The following code is OK.

public static void main( String[] args ) throws IOException, InterruptedException {
        Logger logger = LogUtil.getLogger();

        logger.info("a");
        logger.severe(new Date().toString());
        while (true) {
            logger.info("a");
            logger.severe(new Date().toString());
            Thread.sleep(10000);
        }
    }

Note that blocking doesn’t work System.in.read (), because in a non command line environment, this line of code will not have any running results.

Springboot application itself is a blocking application, so when using winsw tool to create windows service for springboot, this problem will not occur.

Read More: