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

匿名 (未验证) 提交于 2019-12-03 09:19:38

问题:

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?

回答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); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!