8

I am writing a utility to start and stop windows services. The program will be distributed across many computers with differing levels of user privileges so I don't want to use the command line. I've tried using JNA,

import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;

/**
 *
 * @author 
 */
public class WindowsServices {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try
      {

        // TODO code application logic here
         W32ServiceManager serviceManager = new W32ServiceManager();

        W32Service service = serviceManager.openService("uvnc_service", Winsvc.SERVICE_ACCEPT_STOP);
        service.stopService();
        service.close();   
      }
      catch (Exception ex)
      {
          ex.printStackTrace();
      }


    }
}

When I run the program I get the following error

com.sun.jna.platform.win32.Win32Exception: The handle is invalid. at com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77) at windowsservices.WindowsServices.main(WindowsServices.java:26)

Any suggestions would be most helpful.

gavioto
  • 1,695
  • 2
  • 17
  • 38
GEverding
  • 3,131
  • 2
  • 21
  • 23
  • This is pretty cool, didn't know you could control the Windows services this easily with JNA. – tmbrggmn Jul 29 '11 at 18:32
  • The only problem I found with this method is that when you try to stop services you have to provide extra try/catch blocks because some services take a while to go from running to stopped. – GEverding Aug 02 '11 at 12:53

2 Answers2

1

Thanks for the suggestion the author of the question found the error.

import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;

/**
 *
 * @author 
 */
public class WindowsServices {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try
        {
            W32ServiceManager serviceManager = new W32ServiceManager();
            serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS); 
            W32Service service = serviceManager.openService("uvnc_service", Winsvc.SC_MANAGER_ALL_ACCESS);
            service.startService();
            service.close();
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

The error was that the code didn't open the Service Control Manager. I was looking on MSDN and found the process that I needed to follow. I also chanced the permission value, that might also of caused a failure.

gavioto
  • 1,695
  • 2
  • 17
  • 38
0

We use Runtime.getRuntime().exec(command) and then execute the command

cmd /c net start

to start services and

cmd /c net stop

to stop services.

Of course you have to know the service names (and in our case it is DB2 we are after). But this has worked for us.

SVK PETO
  • 115
  • 1
  • 8
Chris Aldrich
  • 1,904
  • 1
  • 22
  • 37