使用 JNA 启动/停止服务

发布于 2024-11-27 03:43:43 字数 1056 浏览 1 评论 0原文

我正在编写一个实用程序来启动和停止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-12-04 03:43:43

感谢问题作者发现错误的建议。

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();
        }
    }
}

错误是代码没有打开服务控制管理器。我在 MSDN 上查找并找到了我需要遵循的流程。我也碰巧了权限值,这也可能导致失败。

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.

征﹌骨岁月お 2024-12-04 03:43:43

我们使用Runtime.getRuntime().exec(command)然后执行命令

cmd /c 网络启动

启动服务并

cmd /c 网络停止

停止服务。

当然,您必须知道服务名称(在我们的例子中,我们要查找的是 DB2)。但这对我们来说很有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文