Access VMware ESXi server with vijava API (your own developed Java software)
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();
}
}
Hallo.
Ich mochte mit Ihrer Website http://www.fireline.de Links tauschen
Hi.
I try to compilate this program on java eclipse, but it is coming mistake :
02 import com.vmware.vim25.CustomFieldDef;
03 import com.vmware.vim25.Permission;
are not uset,or something like.
And with “waitForMe” form
if(task.waitForMe()==Task.SUCCESS)
What can be the problem?
Hi pavel,
what is the exact compiler message? Do you have import the vim library?
Regards Michael
The mistakes are:
The import com.vmware.vim25.Permission is never used
The import com.vmware.vim25.CustomFieldDef is never used
The method waitForMe() from the type Task is deprecated
I just put eclim.
Thanks
Hi, Michael
I am still on the same problem. And I can’t find what kind of problem is this. I think I can’t import the vim library corectly. The program Hello_VM form getstarted is working without problems. I will be happy if you can help me.
Regards Pavel
One more question, should I put also “package” also? And the same like in the “Get Started Tutorial” ?
Finally, I repaired all the mistakes. But the program is still not working. But I will find a way, maybe is from the library.
[...] operation. Exiting…"); } si.getServerConnection().logout(); } } from: Access VMware ESXi server with vijava API (your own developed Java software) I got that I should change "waitForMe()" with "waitForTask()" .. But it still [...]
the main problem is with ESXi 4.0 ..
because no anymore RCLI by the update.