我正在学习使用VirtualBox Java API,但文档并不好。
我的代码:
VirtualBoxManager virtualBoxManager = VirtualBoxManager.createInstance(null);
IVirtualBox vbox = virtualBoxManager.getVBox();
List<IMachine> machines= vbox.getMachines();
for (IMachine m : machines) {
System.out.println("Name: " + m.getName() + " OS: " + m.getOSTypeId());
virtualBoxManager.startVm(m.getName(), null, 7000); //Starting a VM
}
virtualBoxManager.cleanup();
我如何阻止虚拟机?
import org.virtualbox_5_0.*;
public class VirtualboxVMStop {
public static void main(String[] args) {
String machineName = "Centos_1";
String url = "http://localhost:18083";
String user = "test";
String passwd = "test";
VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
mgr.connect(url, user, passwd);
// Get the virtual machine you need to connect to.
IVirtualBox vbox = mgr.getVBox();
IMachine machine = vbox.findMachine(machineName);
ISession session = null;
IProgress prog;
// Determine the status of the virtual machine.
MachineState state = machine.getState();
System.out.println(state);
if (MachineState.Running == state) {
session = mgr.getSessionObject();
machine.lockMachine(session, LockType.Shared);
prog = session.getConsole().powerDown();
prog.waitForCompletion(10000);
session.unlockMachine();
} else {
System.out.println(machineName+" is PoweredOff!");
}
}
}