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.
01.
<
repository
>
02.
<
id
>maven.hyper9.com</
id
>
03.
<
name
>Hyper9 Maven Repository</
name
>
04.
<
url
>http://maven.hyper9.com/repo/<;/
url
>
05.
<
layout
>default</
layout
>
06.
<
releases
>
07.
<
updatePolicy
>never</
updatePolicy
>
08.
<
checksumPolicy
>fail</
checksumPolicy
>
09.
</
releases
>
10.
<
snapshots
>
11.
<
updatePolicy
>always</
updatePolicy
>
12.
<
checksumPolicy
>fail</
checksumPolicy
>
13.
</
snapshots
>
14.
</
repository
>
2. List JWbem as a dependency in your project's POM file.
1.
<
dependency
>
2.
<
groupId
>com.hyper9</
groupId
>
3.
<
artifactId
>jwbem</
artifactId
>
4.
<
version
>0.0.1-SNAPSHOT</
version
>
5.
<
type
>jar</
type
>
6.
<
scope
>compile</
scope
>
7.
</
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.
01.
import
com.hyper9.jwbem.SWbemLocator;
02.
import
com.hyper9.jwbem.SWbemServices;
03.
04.
...
05.
06.
// The IP address or FQDN of the Windows server to connect to.
07.
String serverName =
"hyperv.hyper9.local"
;
08.
09.
// The CIM namespace to connect to.
10.
String cimNamespace =
"root\\virtualization"
;
11.
12.
// The name of the user to connect as. The format of the user name supports
13.
// USERNAME, DOMAIN\\USERNAME, and USERNAME@DOMAIN.
14.
String userName =
"Hyper9\\akutz"
;
15.
16.
// The passprase for the given user.
17.
String passphrase =
"passphrase"
;
18.
19.
// Create a locator object.
20.
SWbemLocator loc =
new
SWbemLocator();
21.
22.
// Connect to the Windows server and return a services object.
23.
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.
01.
import
com.hyper9.jwbem.SWbemObjectSet;
02.
import
com.hyper9.jwbem.msvm.MsvmComputerSystem;
03.
04.
...
05.
06.
// Define the WQL query that returns all of a Hyper-V's virtual machines.
07.
String wql =
"SELECT * FROM Msvm_ComputerSystem WHERE Caption='Virtual Machine'"
;
08.
09.
// Execute the query.
10.
SWbemObjectSet<MsvmComputerSystem> compSysSet = svc.execQuery(wql, MsvmComputerSystem.
class
);
11.
12.
// Print the names of the virtual machines.
13.
for
(MsvmComputerSystem cs : compSysSet)
14.
{
15.
System.out.println(cs.getElementName());
16.
}
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.