Installing JWbem is a simple two step process:
1. Add the Hyper9 Maven Repository to your Maven settings file or to your project's POM file.
<repository> <id>maven.hyper9.com</id> <name>Hyper9 Maven Repository</name> <url>http://maven.hyper9.com/repo/</url> <layout>default</layout> <releases> <updatePolicy>never</updatePolicy> <checksumPolicy>fail</checksumPolicy> </releases> <snapshots> <updatePolicy>always</updatePolicy> <checksumPolicy>fail</checksumPolicy> </snapshots> </repository>
2. List JWbem as a dependency in your project's POM file.
<dependency> <groupId>com.hyper9</groupId> <artifactId>jwbem</artifactId> <version>0.0.1-SNAPSHOT</version> <type>jar</type> <scope>compile</scope> </dependency>
And that's all there is to installing JWbem.
Using JWbem is a fairly straight-forward process.
Here are two quick examples for connecting to a remote Microsoft Windows Hyper-V server and listing its virtual machines.
This example shows how to connect to a remote Hyper-V server's virtualization namespace.
import com.hyper9.jwbem.SWbemLocator; import com.hyper9.jwbem.SWbemServices; ... // The IP address or FQDN of the Windows server to connect to. String serverName = "hyperv.hyper9.local"; // The CIM namespace to connect to. String cimNamespace = "root\\virtualization"; // The name of the user to connect as. The format of the user name supports // USERNAME, DOMAIN\\USERNAME, and USERNAME@DOMAIN. String userName = "Hyper9\\akutz"; // The passprase for the given user. String passphrase = "passphrase"; // Create a locator object. SWbemLocator loc = new SWbemLocator(); // Connect to the Windows server and return a services object. SWbemServices svc = loc.connect(serverName, "127.0.0.1", cimNamespace, username, passphrase);
This example shows how to enumerate all of the virtual machines on the Hyper-V server we established a connection to in the previous example.
import com.hyper9.jwbem.SWbemObjectSet; import com.hyper9.jwbem.msvm.MsvmComputerSystem; ... // Define the WQL query that returns all of a Hyper-V's virtual machines. String wql = "SELECT * FROM Msvm_ComputerSystem WHERE Caption='Virtual Machine'"; // Execute the query. SWbemObjectSet<MsvmComputerSystem> compSysSet = svc.execQuery(wql, MsvmComputerSystem.class); // Print the names of the virtual machines. for (MsvmComputerSystem cs : compSysSet) { System.out.println(cs.getElementName()); }
It is important to remember that when you establish a connection to a remote Windows system with JWbem, all of that system's data is not available to you at once. This is because you must specify a CIM namespace when connecting to the Windows system. Common namespaces include:
To get a Windows system's system properties you should connect to the cimv2 namespace just as if you wish to know about all things Hyper-V you will want to connect to the virtualization namespace.