Using the VI Java API with VMware ESXi

Earlier this year, when I first thought to test out the VI Java API, I estimated I would spend several hours learning the API just to write a simple test program. I was surprised when in less than two hours, I had created my first java program for my ESXi server. Granted it’s a simple program, but the ease and speed of creating something that interacts with my ESXi server was impressive.

VMware ESX Client Program created with the VI Java API

VMware ESX Client Program created with the VI Java API

The program allows you to connect to a ESX server; once logged-on, it displays all the virtual machines you have installed on the server. You then have the option to perform several management operations such as power on, power off, suspend, or restart on any virtual machine. You can download the sample code from the download section.

The VI SDK has a set of management interfaces to the VMware Infrastructure. Everything is centered on Web Services and the VI SDK uses SOAP (Simple Object Access Protocol) as the underlying protocol. The advantage of web services is that it overcomes platform and language dependency. Unfortunately, one issue with web services is that it’s procedural with only methods defined. It doesn’t have built in object orientation. However, the VI Java API can take full advantage of object-oriented programming and compile time–type checking.

The VI SDK object model contains objects called managed objects that represent server-side objects that can be either a manageable entity (eg. virtual machine) or a service (eg. authentication service). One of the things that really helped me when using the VI Java API was the fact that there is a ManagedObject type. It captures all the common properties and behaviors of managed objects and makes things a lot easier to understand and accomplish when just getting started. This object type is not available in the VI SDK.

In the VI SDK, ManagedObjectReference is used to represent managed objects. Further, unlike the norm in an object-oriented programming language, another managed object called PropertyCollector is used to obtain properties of a managed object. The VI Java API hides the PropertyCollector object behind getter methods. This a how a clean API really should be; the programmer shouldn’t have to worry about functionality of other objects to retrieve data for a manged object.

ESX Client Program Sample Code

Sample Code - Connecting to an ESX server and displaying virtual machines and management options

To see just how easy it is to implement some of the functionality look at my code using some of the core API calls below. My methods for such activities as powering on or off a virtual machine are literally one line of code within try/catch blocks. Once the virtual machine managed object is obtained, it’s a piece of cake from there.

ESX Client Program Sample Code - Core API Calls

Sample Code - Core API calls


Tags: , , , , ,

5 Responses to “Using the VI Java API with VMware ESXi”

  1. tstonedout says:

    Thanks – was looking for some kind of java example to interface with ESX. This helps!

  2. [...] have written previous blogs about ESXi (see The Power of Virtualization – a competitive edge and Using the VI Java API with VMware ESXi). You can use ESXi as a standalone server with virtual machines running on it as well as take [...]

  3. nehaparmar16 says:

    Can i use VI SDK API(java) to develop a plugin for vcenter orchestrator using eclipse?? If yes,please let me know how can i do it.


  4. minlu001 says:

    I don’t think this still works with ESXi 5. I get RestrictedVersion exception.
    According to Steve Jin, vijava is not supported on ESXi.


  5. minlu001 says:

    Here is Steve Jin’s comment.

    I guess that last resort to manage VM life cycles on ESXi would be to use ssh or RCLI.


Leave a Reply


8 − = seven