Jur Hillebrink Jur Hillebrink - 1 year ago 119
Java Question

UUID in argument Java servlet Libvirt

I want to call the method for creating a new virtual machine. I have everything except the UUID. How can I insert the random generated UUID in my arguments for calling the method?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

response.getWriter().append("Served at: ").append(request.getContextPath());


public boolean createVM(String vmName,
UUID vmUuid,
long vmMemory,
int vmCpu,
String vmImage) {
String template;

Connect conn;

try {
System.out.println("Connecting to local hypervisor");
conn = new Connect("qemu:///system");

System.out.println("Creating template");
vmUuid = UUID.randomUUID();
template = TEMPLATE;
template = template.replace("$vmName", vmName);
template = template.replace("$vmMemory", String.valueOf(vmMemory));
template = template.replace("$vmCpu", String.valueOf(vmCpu));
template = template.replace("$vmImage", vmImage);
template = template.replace("$vmUuid", vmUuid.toString());

System.out.println("Resulting template: \n" + template);
System.out.println("Creating VM");
Domain domain = conn.domainCreateXML(template, 0);

} catch (LibvirtException e) {
return false;

return true;

Answer Source

This will work:


But make sure to leave out the vmUuid = UUID.randomUUID(); line form inside your method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download