2

i want to create a Windows Service which runs my Java-Application.

This was no problem by using

sc.exe create myService binPath= "java -jar C:\to\my\service.jar"

When I try to start my created service i get this response:

Error 1053: The service did not respond to the start or control request in a timely fashion

Unfortunately it seems my Program didn't respond to the Windows Service that it is running.

How can I communicate to the Windows-Service that my Programm runs?

I tried NSSM which worked great. But I don't want to use another thirdparty app. When I searched for answers I always see that most people use Java Service Wrapper.

How to structure my Java-Code to accept messages from the Windows-Services?

Here is i.e. a simple example of a time-printing programm which i want to run as a service. What do I have to do?

package tst;

import java.util.Date;

public class Tester
{

    public static void main(String[] args)
    {
        Thread thread = new Thread()
        {

            public void run()
            {
                long start = System.currentTimeMillis();
                long end = start + 20 * 1000;

                while (System.currentTimeMillis() < end)
                {
                    Date date = new Date();
                    System.out.println("Time: " + date.toString());

                    try
                    {
                        Thread.sleep(5000);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }

            }
        };
        thread.start();
    }
}
3
  • "I always see that most people use Java Service Wrapper" If most people use that, it probably means you should too, so why don't you? Commented Dec 9, 2016 at 10:30
  • The problem is that i would need a licence but that is no option for me. Commented Dec 9, 2016 at 10:31
  • Did you try JavaExe? It is easier to use. Commented Dec 10, 2016 at 13:03

1 Answer 1

1

Unless your application implements the interface required to interact with the Windows Service Control Manager (SCM), you will not be able to start, stop or manipulate your application as a service. Your only option is to use a service wrapper like NSSM, JSW or even Microsoft's old but still functional Srvany utility.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a predefined interface to implement to my class to support the SCM? Or how can I add that feature to my programm?
This MSDN page lists the functions that must be implemented by a Windows Service. Those functions are likely only available to Java through a native interface of some kind though...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.