With vijava you have the possibility to setup, control and monitor virtual machines with your own developed Java software. A huge disadvantage is that some feature are restricted in VMware ESXi server. That means, that simple features (Create virtual machines, modify and delete virtual machines) are only available in commercial versions of VMware, like VMware Infrastructure. But nevertheless, we are looking forward and I like to show you a sample. :-)

Background information:

vijava 2 is a API was open sourced at sourceforge.net under BSD license in May 2008. The offical website is http://vijava.sourceforge.net/. The vijava API can be used with Eclipse. If you haven’t done so, you can go to http://www.eclipse.org to download and install it. Please follow the tutorial on http://vijava.sourceforge.net/doc/getstarted/tutorial.htm to setup Elipse and create a new project.

The following sample code sends a request to the VMware ESXi server to standby the virtual machine with name Test_VM:

 

import java.net.URL;
import com.vmware.vim25.CustomFieldDef;
import com.vmware.vim25.Permission;
import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.Task;
import com.vmware.vim25.mo.VirtualMachine;  

/**
* http://vijava.sf.net
* @author Steve Jin / Modified by Michael Hopf
*/  

public class VMpowerOps  {   

public static void main(String[] args) throws Exception    {           

String vmname = "Test_VM";
/* other ops: reboot|poweron|poweroff|reset|standby|suspend|shutdown */
String op = "standby";     

ServiceInstance si = new ServiceInstance(new URL("https://server1.fireline.de/sdk"), "root", "password", true);
Folder rootFolder = si.getRootFolder();

VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);

   if(vm==null)
    {
      System.out.println("No VM " + vmname + " found");
      si.getServerConnection().logout();
      return;
    }

    if("reboot".equalsIgnoreCase(op))
    {
      vm.rebootGuest();
      System.out.println(vmname + " guest OS rebooted");
    }
    else if("poweron".equalsIgnoreCase(op))
    {
      Task task = vm.powerOnVM_Task(null);
      if(task.waitForMe()==Task.SUCCESS)
      {
        System.out.println(vmname + " powered on");
      }
    }
    else if("poweroff".equalsIgnoreCase(op))
    {
      Task task = vm.powerOffVM_Task();
      if(task.waitForMe()==Task.SUCCESS)
      {
        System.out.println(vmname + " powered off");
      }
    }
    else if("reset".equalsIgnoreCase(op))
    {
      Task task = vm.resetVM_Task();
      if(task.waitForMe()==Task.SUCCESS)
      {
        System.out.println(vmname + " reset");
      }
    }
    else if("standby".equalsIgnoreCase(op))
    {
      vm.standbyGuest();
      System.out.println(vmname + " guest OS stoodby");
    }
    else if("suspend".equalsIgnoreCase(op))
    {
      Task task = vm.suspendVM_Task();
      if(task.waitForMe()==Task.SUCCESS)
      {
        System.out.println(vmname + " suspended");
      }
    }
    else if("shutdown".equalsIgnoreCase(op))
    {
      Task task = vm.suspendVM_Task();
      if(task.waitForMe()==Task.SUCCESS)
      {
        System.out.println(vmname + " suspended");
      }
    }
    else
    {
      System.out.println("Invalid operation. Exiting...");
    }
    si.getServerConnection().logout();
  }
}