Home > Software Development, vSphere API > Read Environment Variables in Guest Operating System on VMware

Read Environment Variables in Guest Operating System on VMware

My last post explained how to run, kill, and list programs in guest operating system on VMware. In that post, I mentioned that you can actually use the same API, GuestProgramDirector in particular, to read environment variables. I think the explanation is detailed enough for an implementation.

Still, a good sample provides more details. That is why I decided to write a quick sample just to show how to read environment variables. While trying the sample by myself, I did find more that I will discuss after the sample code.

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


Now let’s get to the sample code. Note that I won’t repeat the pre-requisites and tips to run samples, but please check last post.

Copyright (c) 2012 Steve Jin, All Rights Reserved.

package org.doublecloud.vi.vmware.guest.samples;

import guest.GuestProcessDirector;

import java.net.URL;
import java.util.Calendar;

import com.vmware.vim25.GuestProcessInfo;
import com.vmware.vim25.GuestProgramSpec;
import com.vmware.vim25.NamePasswordAuthentication;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.GuestAuthManager;
import com.vmware.vim25.mo.GuestOperationsManager;
import com.vmware.vim25.mo.GuestProcessManager;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class GuestReadEnvVars
  public static void main(String[] args) throws Exception
    ServiceInstance si = new ServiceInstance(new URL(""), "Administrator", "doublecloud.org", true);
    Folder rootFolder = si.getRootFolder();

    ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
    if(mes==null || mes.length ==0)

    VirtualMachine vm = (VirtualMachine) mes[0];

    System.out.println("guest tool status:" + vm.getGuest().toolsRunningStatus);
    if (!"guestToolsRunning".equals(vm.getGuest().toolsRunningStatus))
      System.out.println("The VMware Tools is not running in the Guest OS on VM: " + vm.getName());

    GuestProcessDirector progDir = new GuestProcessDirector(vm, "Administrator", "vijava");

    String[] envVars = progDir.readEnvironmentVariables(new String[] {"PATH", "Cloud", "TEMP", "OS"});

    for(String envVar : envVars)
      int pos = envVar.indexOf("=");
      String name = envVar.substring(0, pos);
      String value = envVar.substring(pos+1);
      System.out.println("name: " + name);
      System.out.println("value: " + value);



If you put an invalid (non-existing) environment variable name, like Cloud in the sample, to the readEnvironmentVariables() method, it doesn’t complain but silently ignore it. The returned list contains only those that exist, therefore the number of returned environment variables could be less than what you passed in.

Although the order of the returned variables are the same as the order of the names you pass in, it’s not explicitly specified in API spec. To be safe, you’d better not code based on this assumption.

  1. santhosh
    November 12th, 2012 at 06:30 | #1

    Hi Steve , this VI java really helpful to me. Can you explain what percentage resources using a process which is running on Guest Virtual machine

    Thank you,

  2. November 12th, 2012 at 11:53 | #2

    Thanks Santhosh, I am glad you like it. Did you mean to find out the percentage of CPU used by a process running in a guest OS?

  3. santhosh
    November 14th, 2012 at 01:27 | #3

    @Steve Jin

    Thanks Steve for replying to me , I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine.

    Thanks in advance

  4. November 14th, 2012 at 01:47 | #4

    You have to run platform specific commands. On Windows, for example, you can use wmic command like this: (http://serverfault.com/questions/374378/cpu-usage-while-running-command-in-command-line)
    wmic path win32_perfFormattedData_perfProc_process get Name, PercentProcessorTIme
    You can also write a program to get whatever you want.
    On Linux, you can try ps -p -o %cpu,%mem (http://stackoverflow.com/questions/1221555/how-can-i-get-the-cpu-usage-and-memory-usage-of-a-single-process-on-linux-ubunt)

  5. santhosh
    November 14th, 2012 at 02:48 | #5

    @Steve Jin

    I need to find CPU and Memory utilization of a process which is running on Guest Virtual Machine from vCenter using VI java.

  6. November 14th, 2012 at 14:37 | #6

    There is no direct support for that in vSphere API. You want to run commands (see my previous reply) in guest operating systems for the information you want.

  7. Srinivasan V N
    February 1st, 2013 at 01:20 | #7

    Plesae could you tell, where i have to download jar file for below class.


  8. Mark Norman
    August 22nd, 2013 at 09:17 | #9

    I can use GuestProcessDirector successfully with Windows guests, but with Linux guests, I always get this exception: com.vmware.vim25.InvalidGuestLogin.
    The login info I am supplying is correct and the Linux guest has VMware tools installed.


  9. August 21st, 2015 at 02:18 | #10

    That is very fascinating, You are an overly professional blogger.

    I’ve joined your feed and stay up for in quest of more of your fantastic post.

    Additionally, I have shared your website in my social networks

  10. September 16th, 2015 at 01:12 | #11

    I blog quite often and I genuinely appreciate your content.

    The article has really peaked my interest. I am going to book mark your blog and keep
    checking for new details about once a week.
    I opted in for your Feed too.

  11. September 18th, 2016 at 09:40 | #12

    Appreciating the hardd work you put into your blog and detailed information you
    provide. It’s great to come across a blog every once in a while that isn’t
    the same unwanted rehashed material. Wonderful read!

    I’ve saved your site and I’m adding your RSS feeds to my Google account.

  1. March 7th, 2012 at 01:41 | #1
  2. March 19th, 2012 at 02:45 | #2