从java应用程序运行在vmware工作站中创建的Vm

时间:2015-05-29 22:47:38

标签: java virtual-machine vmware virtualization

我对vmware知之甚少。 我找到了启动vm的代码。但我需要有关的信息  网址+用户名+密码。 我怎样才能获得这些数据?

     package com.vmware.vim25.mo.samples.vm;

        import java.net.URL;

        import com.vmware.vim25.mo.Folder;
        import com.vmware.vim25.mo.InventoryNavigator;
        import com.vmware.vim25.mo.ServiceIns

tance;
    import com.vmware.vim25.mo.Task;
    import com.vmware.vim25.mo.VirtualMachine;

    /**
     * http://vijava.sf.net
     * @author Steve Jin
     */

    public class VMpowerOps 
    {
      public static void main(String[] args) throws Exception 
      {
        if(args.length!=5)
        {
          System.out.println("Usage: java VMpowerOps <url> " +
                "<username> <password> <vmname> <op>");
          System.out.println("op - reboot|poweron|poweroff" +
                "|reset|standby|suspend|shutdown");
          System.exit(0);
        }

        String vmname = args[3];
        String op = args[4];

        ServiceInstance si = new ServiceInstance(
            new URL(args[0]), args[1], args[2], 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();
      }
    }

1 个答案:

答案 0 :(得分:1)

您引用的代码适用于VMware vSphere(ESX或vCenter - 企业服务) - 而不是基于桌面的VMware Workstation。

有关其他参考,请参阅您在示例顶部找到的网址:http://vijava.sourceforge.net/。另见:https://developercenter.vmware.com/-/connect-to-vsphere-from-a-java-program-getting-started-

对于VMware Workstation,看起来您应该能够使用vmrun。可在https://www.vmware.com/support/developer/vix-api/vix112_vmrun_command.pdf处获取文档。我还在http://www.virtualizationadmin.com/articles-tutorials/vmware-esx-and-vsphere-articles/managing-workstation-and-vsphere-vmrun-command.html找到了另一篇文章。