Deploying from Template using VMWare Infrastructure Java API

I'm using the VI Java API for VMWare and the vCenter 5.0.0.  I can successfully clone a running VM but would like to clone/deploy a VM from template.  However, when I specify a template to clone instead of a VM I get this:

Clone virtual machine

A specified parameter was not correct.  

Not sure how to specify or if I even have a resource pool.  Here's my code:

public void cloneVM() throws Exception{
			System.out.println("inClon<wbr ></wbr>eVM");

		    String vmname = "chefDemo232_Template";
		    String cloneName = "template clone test";
		    ServiceInstance si = new ServiceInstance(
		        new URL("my URL"), "my name", "my password", true);

		    System.out.println("afterU<wbr ></wbr>rl");
		    Folder rootFolder = si.getRootFolder();
		    VirtualMachine vm = (VirtualMachine) new InventoryNavigator(
		        rootFolder).searchManagedE<wbr ></wbr>ntity(
		            "VirtualMachine", vmname);
		 //   Datacenter dc = (Datacenter) new InventoryNavigator(
		        //    rootFolder).searchManagedE<wbr ></wbr>ntity("Dat<wbr ></wbr>acenter", dcName);
		 //   ResourcePool rp = (ResourcePool) new InventoryNavigator(
		           // dc).searchManagedEntities(<wbr ></wbr>"ResourceP<wbr ></wbr>ool")[0];

		      System.out.println("No VM " + vmname + " found");
		      si.getServerConnection().l<wbr ></wbr>ogout();

		    VirtualMachineCloneSpec cloneSpec = 
		      new VirtualMachineCloneSpec();<wbr ></wbr>
		    cloneSpec.setLocation(new VirtualMachineRelocateSpec<wbr ></wbr>());
		    cloneSpec.setPowerOn(false<wbr ></wbr>);
		    cloneSpec.setTemplate(fals<wbr ></wbr>e);

		    Task task = vm.cloneVM_Task((Folder) vm.getParent(), 
		        cloneName, cloneSpec);
		    System.out.println("Launch<wbr ></wbr>ing the VM clone task. " +
		    		"Please wait ...");

		    String status = task.waitForMe();
		      System.out.println("VM got cloned successfully.");
		      System.out.println("Failur<wbr ></wbr>e -: VM cannot be cloned");

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Uni KittyCommented:
Hi Hyper,

I'm not a programmer but I have some VMware resources that may help you troubleshoot this problem.

First have a look at the vSphere 5.0 SDK guides if you haven't already, the location.pool is only required for templates, which is probably why it works for a running VM if you're not specifying it.

Check out:

Check out this fling tool from VMware, called Onyx, you can use this on the vCenter Server and have Onyx launch the vSphere client, then it will output the XML of whatever tasks you're doing. you might find this useful. It will tell you what things should look like.

Good luck!
Uni KittyCommented:
Hi There! Just checking back to see if you were able to figure it out?
HyperBPPAuthor Commented:
Yeah, sorry for the delay.  This is for work so my job dictates my schedule on when I can work on stuff.  I figured it out:

 VirtualMachine vm = (VirtualMachine) new InventoryNavigator(
                         "VirtualMachine", vmname);
                 Datacenter dc = (Datacenter) new InventoryNavigator(
                         rootFolder).searchManagedEntity("Datacenter", dcName);
                 ResourcePool rp = (ResourcePool) new InventoryNavigator(



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Uni KittyCommented:
Great! I'm glad you got it working!
HyperBPPAuthor Commented:
The actual solution which the expert comment led me too is shown in my answer.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.