开发者

Recommended libraries/howtos for using WMI with java? [closed]

开发者 https://www.devze.com 2023-02-02 11:25 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendatio开发者_如何转开发ns for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 8 years ago.

Improve this question

Hey Folks. I have a reasonably large commercial project (so for-pay licensing is always an option) that is moving into the windows space and I am planning on doing alot of polling with WMI. This is a Java 1.6 client app. Are there any good resources or books out there to get me started in accomplishing this in java? I feel my .NET fu is pretty strong, but I often don't know where to start to accomplish the same tasks in Java.

Thanks!


I don't know of any books specific to Java, but If I can offer some advice it would be to just start with WMI MSDN documentation. Also, since WMI is a COM based API, I would look into using a Java to COM bridge for accessing the API. Any one of the following should work:

  • Java2Com
  • JACOB - A WMI example.
  • J-Integra - A WMI example.
  • J-Interop - A WMI example.
  • Another Java2Com tool

For a Java wrapper around WMI, there is the jWMI library.


I would like to spread some word of warning regarding the jWMI: in case you do not have control over the systems where your app will run (say simple desktop application), expect issues with the antiviruses. jWMI is not a real library, but merely a utility. It creates a jwmi.vbs file on flight and simply tries to execute it with the "cmd /c" Runtime call. As you might imagine, the antiviruses are typically not happy with such behaviour and sandbox/block the file immediately.


Below is an example of using Jawin to get the system uptime using WMI.

To run the code, you will need to download Jawin library and add jawin.dll to your eclipse project root

public static void main(String[] args) throws COMException {
String computerName = "";
String userName = "";
String password = "";
String namespace = "root/cimv2";

String queryProcessor = "SELECT * FROM Win32_OperatingSystem";

DispatchPtr dispatcher = null;

try {

    ISWbemLocator locator = new ISWbemLocator(
            "WbemScripting.SWbemLocator");
    ISWbemServices wbemServices = locator.ConnectServer(computerName,
            namespace, userName, password, "", "", 0, dispatcher);
    ISWbemObjectSet wbemObjectSet = wbemServices.ExecQuery(
            queryProcessor, "WQL", 0, null);
    DispatchPtr[] results = new DispatchPtr[wbemObjectSet.getCount()];
    IUnknown unknown = wbemObjectSet.get_NewEnum();
    IEnumVariant enumVariant = (IEnumVariant) unknown
            .queryInterface(IEnumVariant.class);

    enumVariant.Next(wbemObjectSet.getCount(), results);

    for (int i = 0; i < results.length; i++) {
        ISWbemObject wbemObject = (ISWbemObject) results[i]
                .queryInterface(ISWbemObject.class);

        System.out.println("Uptime: "
                + wbemObject.get("LastBootUpTime"));
    }
} catch (COMException e) {
    e.printStackTrace();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消