There is a recent question in vSphere(VI) Java API forum about this. On its face, it’s very easy because most people know how to get hold of the version as follows:
String version = si.getAboutInfo().getVersion();
The si in the above code is the variable of ServiceInstance object. If you have never used the API yet, please try this Getting Started Tutorial which shows how to get your first program running from scratch in 5 minutes.
Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.
If you are connecting to a vCenter server and try to get the version of a HostSystem the vCenter manages, it’s not so obvious. But it’s definitely doable. Here is the solution assuming you already get hold of the HostSystem object as host variable here:
String version = host.getConfig().getProduct().getVersion();
Here you know why. First, the aboutInfo is now called product although they are of the same type. Second, it’s hidden within the config property.
Before taking the code away, I would like to share with you an important tip for better performance. You should consider the following code:
String version = (String) host.getPropertyByPath(“config.product.version”);
AboutInfo ai = ((AboutInfo) host.getPropertyByPath(“config.product”); String version = ai.getVersion();
Because config object could be huge and it takes time for the vCenter to grab the data and serialize it to XML, and for VI Java API to de-serialize XML back to data object. You can expect getConfig() call take about one second to finish. Most of that time is on vCenter side for grabbing data. So to avoid this, you can just fetch the sub-properties with much faster speed.
This tip works as well if you connect to ESX directly.