Advertisement
noteirak

VirtualBox API - Start VM in Headless mode

Mar 20th, 2015
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. /*
  2.  * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  3.  * Version 2, December 2004
  4.  *
  5.  * Everyone is permitted to copy and distribute verbatim or modified
  6.  * copies of this license document, and changing it is allowed as long
  7.  * as the name is changed.
  8.  *
  9.  * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  10.  * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  11.  *
  12.  * 0. You just DO WHAT THE FUCK YOU WANT TO.
  13.  */
  14.  
  15. import org.virtualbox_4_3.IMachine;
  16. import org.virtualbox_4_3.IProgress;
  17. import org.virtualbox_4_3.ISession;
  18. import org.virtualbox_4_3.VirtualBoxManager;
  19.  
  20. /**
  21.  * The following piece of code assume that
  22.  * - WebServices server is running
  23.  * - Authentication is disabled
  24.  *
  25.  * To disable authentication, run on the host before starting the server:
  26.  *
  27.  * vboxmanage setproperty websrvauthlibrary null
  28.  *
  29.  */
  30. public class StartVM {
  31.  
  32.    public static void main(String[] args) {
  33.       VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
  34.       vboxManager.connect("http://localhost:18083", "", "");
  35.  
  36.       try {
  37.          IMachine machine = vboxManager.getVBox().findMachine("machineName");
  38.          ISession session = vboxManager.getSessionObject();
  39.          IProgress p = machine.launchVMProcess(session, "headless", null);
  40.          try {
  41.             p.waitForCompletion(-1);
  42.             if (p.getResultCode() != 0) {
  43.                System.out.println("Machine failed to start: " + p.getErrorInfo().getText());
  44.             }
  45.          } finally {
  46.             session.unlockMachine();
  47.          }
  48.       } finally {
  49.          vboxManager.disconnect();
  50.          vboxManager.cleanup();
  51.       }
  52.    }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement