Reputation: 3371
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?
Upvotes: 1
Views: 3742
Reputation: 18468
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);
Upvotes: 1