Java remotely manages Windows services with WMI4J

Source: Internet
Author: User
Tags maven central

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.