In the original, please visit: http://www.chenlichao.cn/opensource/74.html
WMI4J is a pure Java-implemented Windows WMI client that is re-encapsulated for WMI based on J-interop, providing a more convenient way to meet basic Windows management, including service management, performance queries, execution scripts, and more.
wmi4j Download
With Maven's friends can be introduced directly, Groupid=cn.chenlichao, artifactid=wmi4j, version=0.9. SOURCE Address: github:https://github.com/chenlichao-cn/wmi4j use other widget frame friends, you can go to Maven Central Library or http://maven.oschina.net query wmi4j, of course, remember to download its dependency package:
org.glassfish.main.external:j-interop-repackaged:4.0
org.slf4j:slf4j-api:1.7.7
org.apache.commons:commons-lang3:3.1
using WMI4J to manage Windows services
With wmi4j management is very convenient, a small amount of code can complete the function, below we use a practical example to demonstrate how to get a list of services, query services, get the specified service status, start the service and stop the service several of the most commonly used features.
For WMI-related documentation, please refer to Microsoft Official documentation: WMI Reference
Public static void main (String[] args) { //Set Connection parameters string server = "192.168.1.201"; string username = " Administrator "; string password = " password "; string namespace = "root\\cimv2"; //Build connector swbemlocator locator = new swbemlocator (server,username,password,namespace); try { //Connecting a remote server swbemservices wbemservices = locator.connectserver (); //Traversal Service list SWbemObjectSet services = Wbemservices.instancesof ("Win32_Service"); system.out.println ("Number of services: " + Services.getcount ()); iterator<swbemobject> iterator = services.iterator (); while (Iterator.hasnext ()) { swbemobject service = iterator.next (); system.out.println (Service.getobjecttext ()); system.out.println ("-----------------------------------------------"); break; //a lot of service, just hit one. } //querying services at the beginning of Windows swbemobjectset winservices = wbemservices.execquery ( "Select * from win32_service where displayname like ' windows% '); system.out.println ("Number of services beginning with Windows:" + Winservices.getcount ()); //direct access to services through the service name. //Note: The service name is not the name shown in the Services.msc list, the name shown is DisplayName property, //and the Get method must use the primary key property name. For example: Application management service, //is shown in Services.msc, &NBSP; But its service name is appmgmt, can be viewed through properties. swbemobject dhcpclient = wbemservices.get ("Win32_service.name= ' AppMgmt '"); &NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Service Name:" + dhcpclient.getpropertybyname ("name") .getstringvalue ()); SYSTEM.OUT.PRINTLN ("Display Name:" + dhcpclient.getpropertybyname ("DisplayName") .getstringvalue ()); // Gets the service status system.out.println ("Status: " + dhcpclient.getpropertybyname ("state"). GetStringValue ()); //Start service Dhcpclient.execmethod ("StartService"); System.out.println ("Post-boot status: " + wbemservices.get ("Win32_service.name= ' Appmgmt '"). Getpropertybyname ("state"). GetStringValue ()); //Stop service Dhcpclient.execmethod ("StopService"); System.out. println ("state: after stopping again" + wbemservices.get ( "Win32_service.name= ' Appmgmt '") . Getpropertybyname ("state"). GetStringValue ()); } catch (wmiexception e) { e.printstacktrace (); } catch ( unknownhostexception e) { e.printstacktrace (); }}
This article is from the "Son" blog, please make sure to keep this source http://leaze.blog.51cto.com/83088/1559263
Java remotely manages Windows services with WMI4J