使用 JNA 启动/停止服务
我正在编写一个实用程序来启动和停止 Windows 服务。该程序将分布在许多具有不同用户权限级别的计算机上,因此我不想使用命令行。我尝试使用 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();
}
}
}
当我运行程序时,出现以下错误
com.sun.jna.platform.win32.Win32Exception:句柄无效。 在 com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77) 在 windowsservices.WindowsServices.main(WindowsServices.java:26)
任何建议都是最有帮助的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢问题作者发现错误的建议。
错误是代码没有打开服务控制管理器。我在 MSDN 上查找并找到了我需要遵循的流程。我也碰巧了权限值,这也可能导致失败。
Thanks for the suggestion the author of the question found the error.
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.
我们使用
Runtime.getRuntime().exec(command)
然后执行命令启动服务并
停止服务。
当然,您必须知道服务名称(在我们的例子中,我们要查找的是 DB2)。但这对我们来说很有效。
We use
Runtime.getRuntime().exec(command)
and then execute the commandto start services and
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.