java 关闭虚拟机_如何使用Oracle VirtualBox Java API关闭虚拟机?
我學習使用VirtualBox
Java API,但文檔并不好.
我的代碼:
VirtualBoxManager virtualBoxManager = VirtualBoxManager.createInstance(null);
IVirtualBox vbox = virtualBoxManager.getVBox();
List 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!");
}
}
}
總結
以上是生活随笔為你收集整理的java 关闭虚拟机_如何使用Oracle VirtualBox Java API关闭虚拟机?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: folderutils.java_值得分
- 下一篇: objective-c java_程序员