'JNA:com.sun.jna.platform.win32.Win32Exception- access denied

I use following code to control a windows service from java program

public class PostgresService2
{
    public static void main(String[] args)
    {
        try
            {
                W32ServiceManager serviceManager = new W32ServiceManager();
                serviceManager.open(Winsvc.SERVICE_STOP);
                W32Service service = serviceManager.openService("DBService",
                        Winsvc.SERVICE_ACCEPT_STOP);
                service.stopService();
                service.close();
             }
             catch (Exception ex)
             {
                 ex.printStackTrace();
             }
    }
}

This gives error on windows7 (64bit) machine

com.sun.jna.platform.win32.Win32Exception: Access is denied.
        at com.sun.jna.platform.win32.W32Service.queryStatus(W32Service.java
        at com.sun.jna.platform.win32.W32Service.waitForNonPendingState(W32S
        at com.sun.jna.platform.win32.W32Service.stopService(W32Service.java
        at chs.capitalmigrate.ui.PostgresService2.main(PostgresService2.java

The shell from where the command is run has administrative privileges. How I can provide full access?



Solution 1:[1]

I am using SC_MANAGER_ALL_ACCESS and that works. Not sure if that helps.

 W32ServiceManager serviceManager = new W32ServiceManager();
               serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
               W32Service service = serviceManager.openService("servicename", Winsvc.SC_MANAGER_ALL_ACCESS);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jayan