Start a Windows 10 service by Java code

Asked

Viewed 709 times

0

In Windows 7 I used the following method to start a Windows service in Java:

static final String CMD_START = "cmd /c net start \"";

public static int startService(String serviceName) throws Exception {
  return execCmd(CMD_START + serviceName + "\"");
}

However, after I upgraded to Windows 10 this method no longer works. Could help me with a code that starts a Windows service and works on Windows 10?

  • I believe that the best tool to manage services via command line is SC. However, I do not know if it is available in Windows 10 (I think so). See: http://stackoverflow.com/questions/133883/stop-and-start-a-service-via-batch-or-cmd-file

  • I tried some of these command and it didn’t work on Windows 10... =/

  • what error Windows 10 presents?

  • When trying to start the Tomcat service for example, this appears on the console: The Apache Tomcat service 7.0 Tomcat7 was not started. For more help, type NET HELPMSG 3521.

  • And you can start normally through the Windows 10 service window, correct?

  • I can, yes, normally!

Show 1 more comment

1 answer

-1

To initiate any OS command, the code to be used is:

Runtime r = Runtime.getRuntime();
r.exec("CMD");

The command must be in the PATH of the operating system.

If the above code along with the correct PATH configuration does not allow the execution, then there is some probability that it is related to permission, so it would look for other topics showing exactly this context: OS administrator is not allowed to start a service in windows 10.

  • 1

    Excuse the question colleague, but how do you put the "command in the classpath"? I’ve never seen this expression before.

  • I missed the term, it’s just PATH.

  • I thought it was, but in PATH you put directories only, no? How do you put a command there?

  • Let’s forget about Java for now. Try starting your service from the terminal. Make sure it started normal. If you have not started, the problem is not in the code but in the OS configuration.

  • I tried to start directly from CMD, tried to start the Tomcat service for example by typing: net start "Tomcat7" E appeared: System Error 5. Access denied. What could be?

  • So the problem is not the Java code, the problem is in the system configuration. Your user is part of the OS administrators?

  • Yes, my user is an OS administrator.

  • Then it would be good to close this question because it is not a problem in the Java code itself, and search in other topics showing exactly this context: Administrator of the OS is not allowed to start a service in windows 10.

  • True colleague. But I did. I put the eclipse as administrator and it worked perfectly! Thanks for the help!

  • So congratulations!

  • I believe @Andréhenriques can change its answer, including the Administrator tip. So the answer can be accepted as the correct one.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.