Is there any way, in java, to check on the status of a windows service? [closed]

*爱你&永不变心* 提交于 2019-12-30 06:43:09

问题


I am looking for a library that will allow me to look up the status of a windows service to verify that the service is started and running. I looked into the Sigar library, but it is GPL and therefor I cannot use it. A Commercial or BSD(ish) license is required as this will be bundled into commercial software.


回答1:


If nothing else helps, try to think of a slightly different approach (if you can, of course), e.g.:

  • There is a plenty of free/non-free software which does monitoring, including Windows service monitoring (e.g. nagios, Zabbix, etc.). These monitors typically have open API where your Java app could integrate into in a number of different ways.
  • If you have the control over depending service application, expose another, different way for your Java application to check (e.g. run a dummy listener on a port, create a file, etc.). Windows services aren't a cross-platform thing therefore is not something you would expect to be supported anytime soon.



回答2:


I don't think there is any pure-Java way to do this because some operating systems don't have the notion of "services" like Windows does. In our projects, we wrote a wrapper around calls to the "sc" command from the command line. To get the status of a service, you can do:

sc \\some-computer query "my service name"

You'll have to manually parse the output but it's pretty straightforward.




回答3:


I don't know of any libraries, but depending on how detailed you need to get you might get by with some shell commands and parsing the output.

NET START servicename

will either start the service, or give you back an error message that tells you its already started. I don't know of any command that will just give you the status though.



来源:https://stackoverflow.com/questions/127299/is-there-any-way-in-java-to-check-on-the-status-of-a-windows-service

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