Home > vSphere API > 5 Easy Steps Using vSphere Java API

5 Easy Steps Using vSphere Java API

In my previous blogs, I have introduced the vSphere API object model, vSphere Java API architecture. I assume you’ve run through the 5 minute Getting Started Tutorials with HelloWorld sample.

Now, let’s take a look on how to use the API in general.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure


1. Always starts with a ServiceInstance with URL/username/password, or URL/sessionID. For example,

ServiceInstance si = new ServiceInstance(new URL(urlStr), username, password, true);

2. From the ServiceInstance object, you can:

  • Get its properties like capability:

Capability cap = si.getCapability();

  • Get root folder object of the inventory tree.

Folder rootFolder = si.getRootFolder();

  • Since property content (ServiceContent) only holds AboutInfo and ManagedObjectReferences to all different manager, we move the these info up by providing methods like getAboutInfo and various get*() methods to get “manager” objects (including SearchIndex) directly. For example,

SearchIndex searchIndex = si.getSearchIndex();
EventManager em = si.getEventManager();

3. With the manager objects returned from ServiceInstance object, you can do many different things. For example: list all the events with a VM by using EventManager.

4. From the root Folder or any ManagedEntity in the inventory,

  • You can navigate to the sub-nodes in the inventory tree:


  • You can use InventoryNavigator class to search based on your criteria.
  • All the items you see in the inventory tree are of type ManagedEntity.
  • You can test the exact type of a ManagedEntity

if(me instanceof VirtualMachine){ }

  • You can call the methods defined on the exact subtypes of the ManagedEntity.

Task task = ((VirtualMachine)me).powerOffVM_Task();

5. Lastly, don’t forget to¬† logout the session when you are done. Exiting your application without logging out may hurt your server performance. (see, vSphere API best practice #9, and tips on session management).


To get help, you can find exact description of each managed object and its properties/methods in the vSphere SDK API reference. Just look for the managed object type and its method with the same name. Please skip the first _this parameter and the rest are the same except the difference of real type and ManagedObjectReference. The description should be applicable despite the difference.

You will find some samples inside the com.vmware.vim25.mo.samples package. As you will find out, the samples using this APIs are much shorter and more readable than the ones using Web Service interfaces.

Categories: vSphere API Tags: ,
  1. Nikhil
    July 25th, 2015 at 21:47 | #1

    The samples link is not working.. I am getting connection was reset error.

  2. July 26th, 2015 at 14:44 | #2

    Thanks Nikhil, the SF.net is updating its infrastructure. It may have broken the links. Try it later. Sorry about the inconvenience.

  1. No trackbacks yet.