使用Java API Oracle VirtualBox时出现异常
我的目标是使用Java在VirtualMachine中运行一些进程。首先,我有一部分用于创建和连接VB的代码,但在21行和89行中有异常(主线程中的空指针)。我正在寻找一些解决这个问题的答案,并阅读了这个问题,看起来我没有Oracle VB。但在我的电脑里,我有它,版本相当于导入的。 因此,如果您有使用此API的经验,或者可以帮助我,请详细描述我如何修复它。因此,我的代码:使用Java API Oracle VirtualBox时出现异常,java,api,virtualbox,Java,Api,Virtualbox,我的目标是使用Java在VirtualMachine中运行一些进程。首先,我有一部分用于创建和连接VB的代码,但在21行和89行中有异常(主线程中的空指针)。我正在寻找一些解决这个问题的答案,并阅读了这个问题,看起来我没有Oracle VB。但在我的电脑里,我有它,版本相当于导入的。 因此,如果您有使用此API的经验,或者可以帮助我,请详细描述我如何修复它。因此,我的代码: import org.virtualbox_5_1.*; import org.virtualbox_5_1.ISessi
import org.virtualbox_5_1.*;
import org.virtualbox_5_1.ISession;
import org.virtualbox_5_1.IProgress;
import java.util.List;
import java.util.Arrays;
public class Events_5_1 {
static VirtualBoxManager mgr;
static Thread listener;
public static void main(String[] args) {
String vmName = Long.toString(System.currentTimeMillis()); // Берем рандомное значение имени VirtualMachine
System.out.println("Creating VirtualBox instance");
mgr = VirtualBoxManager.createInstance(null);
try {
listener = new EventWorker();
listener.start();
try {
//Создаем пустую машину и сохраняем на диск
IMachine vm = mgr.getVBox().createMachine(null, vmName, null, "Other", null);//тестить , разобраться с параметрами
vm.saveSettings();
mgr.getVBox().registerMachine(vm);
vm = mgr.getVBox().findMachine(vmName);
ISession session = mgr.getSessionObject();
IProgress p = vm.launchVMProcess(session, "headless", null); // Вместо headless - ставим процесс ??
p.waitForCompletion(-1);
try {
if (p.getResultCode() != 0) {
throw new RuntimeException(p.getErrorInfo().getText());
} else {
p = session.getConsole().powerDown();
p.waitForCompletion(-1);
if (p.getResultCode() != 0) {
throw new RuntimeException(p.getErrorInfo().getText());
} else {
}
}
} finally {
session.unlockMachine();
while (!SessionState.Unlocked.equals(vm.getSessionState())) {
try {
System.out.println("Waiting for session unlocked");
Thread.sleep(1000L);
} catch (InterruptedException e) {
System.err.println("Interrupted while vaiting for session unlocked");
}
}
System.out.println("Deleting machine");
vm.deleteConfig(vm.unregister(CleanupMode.DetachAllReturnHardDisksOnly));
}
} finally {
listener.interrupt();
try {
listener.join(5000);
} catch (InterruptedException e) {
System.err.println("Inerrupted while vaiting for EventWorker stop");
}
if (listener.isAlive()) {
System.err.println("Event worked did not stop in a timely fashion");
} else {
System.out.println("event worker stoped");
}
}
} finally {
mgr.disconnect();
mgr.cleanup();
System.out.println("Disconecting from VirtualBox");
}
}
static class EventWorker extends Thread {
IEventListener el;
@Override
public void run() {
System.out.println("EventWorker started");
el = mgr.getVBox().getEventSource().createListener();
//TODO: connect gradle, mvnrepository.com idea connect datasource postgre
List<VBoxEventType> types = Arrays.asList(VBoxEventType.OnSessionStateChanged, VBoxEventType.OnMachineStateChanged,
VBoxEventType.OnMachineRegistered);
mgr.getVBox().getEventSource().registerListener(el, types, false);
try{
while(!isInterrupted()){
mgr.waitForEvents(0);
IEvent rawEvent = mgr.getVBox().getEventSource().getEvent(el , 1000);
if(rawEvent==null) continue;
try{
System.out.println("Got event type "+rawEvent.getType());
if(VBoxEventType.OnSessionStateChanged.equals(rawEvent.getType())){
ISessionStateChangedEvent event = ISessionStateChangedEvent.queryInterface(rawEvent);
System.out.println("Machine "+event.getState()+" for machine "+event.getMachineId());
}
if(VBoxEventType.OnMachineRegistered.equals((rawEvent.getType()))){
IMachineRegisteredEvent event = IMachineRegisteredEvent.queryInterface(rawEvent);
System.out.println("Machine "+event.getMachineId()+" has been "+(event.getRegistered() ? "registered":"unregistered"));
}
if(VBoxEventType.OnMachineStateChanged.equals(rawEvent.getType())){
IMachineStateChangedEvent event = IMachineStateChangedEvent.queryInterface(rawEvent);
System.out.println("Machine "+event.getMachineId()+" state changed to "+event.getState());
}
}finally {
mgr.getVBox().getEventSource().eventProcessed(el,rawEvent);
}
}
}catch(Throwable t){
t.printStackTrace();
}finally {
mgr.getVBox().getEventSource().unregisterListener(el);
System.out.println("EventWorker finished");
}
}
}
}
import org.virtualbox\u 5\u 1.*;
导入org.virtualbox\u 5\u 1.ISession;
导入org.virtualbox_5_1.IProgress;
导入java.util.List;
导入java.util.array;
公开课活动{
静态VirtualBoxManager经理;
静态线程侦听器;
公共静态void main(字符串[]args){
字符串vmName=Long.toString(System.currentTimeMillis())//虚拟机
System.out.println(“创建VirtualBox实例”);
mgr=VirtualBoxManager.createInstance(null);
试一试{
listener=neweventworker();
listener.start();
试一试{
//Создаем пустую машину и сохраняем на диск
IMachine vm=mgr.getVBox().createMachine(null,vmName,null,“其他”,null);//
vm.saveSettings();
管理器getVBox().registerMachine(vm);
vm=mgr.getVBox().findMachine(vmName);
ISession session=mgr.getSessionObject();
IProgress p=vm.launchVMProcess(会话,“headless”,null);//БМССССиheadless-Сааааиаааааа107??
p、 等待完成(-1);
试一试{
如果(p.getResultCode()!=0){
抛出新的RuntimeException(p.getErrorInfo().getText());
}否则{
p=session.getConsole().powerDown();
p、 等待完成(-1);
如果(p.getResultCode()!=0){
抛出新的RuntimeException(p.getErrorInfo().getText());
}否则{
}
}
}最后{
session.unlockMachine();
而(!SessionState.Unlocked.equals(vm.getSessionState())){
试一试{
System.out.println(“等待会话解锁”);
睡眠(1000L);
}捕捉(中断异常e){
System.err.println(“在会话解锁时变为中断”);
}
}
System.out.println(“删除机器”);
vm.deleteConfig(vm.unregister(CleanupMode.DetachAllReturnHardDisksOnly));
}
}最后{
interrupt();
试一试{
加入(5000);
}捕捉(中断异常e){
System.err.println(“在为EventWorker停止而求值时中断”);
}
if(listener.isAlive()){
System.err.println(“工作的事件没有及时停止”);
}否则{
System.out.println(“事件工作者停止”);
}
}
}最后{
经理断开连接();
清理经理();
System.out.println(“断开与VirtualBox的连接”);
}
}
静态类EventWorker扩展线程{
IEventListener el;
@凌驾
公开募捐{
System.out.println(“EventWorker已启动”);
el=mgr.getVBox().getEventSource().createListener();
//TODO:connect gradle,mvnrepository.com idea connect数据源postgre
列表类型=Arrays.asList(VBoxEventType.OnSessionStateChanged,VBoxEventType.OnMachineStateChanged,
VBoxEventType.on机器注册);
mgr.getVBox().getEventSource().registerListener(el,types,false);
试一试{
而(!isInterrupted()){
waitForEvents经理(0);
IEvent rawEvent=mgr.getVBox().getEventSource().getEvent(el,1000);
如果(rawEvent==null)继续;
试一试{
System.out.println(“获取事件类型”+rawEvent.getType());
if(VBoxEventType.OnSessionStateChanged.equals(rawEvent.getType())){
ISessionStateChangedEvent事件=ISessionStateChangedEvent.queryInterface(rawEvent);
System.out.println(“Machine”+事件.getState()+”代表Machine”+事件.getMachineId());
}
if(VBoxEventType.OnMachineRegistered.equals((rawEvent.getType())){
IMachineRegisteredEvent=IMachineRegisteredEvent.queryInterface(rawEvent);
System.out.println(“Machine”+事件.getMachineId()+”已“+(event.getRegistered()?”已注册):“未注册”);
}
if(VBoxEventType.OnMachineStateChanged.equals(rawEvent.getType())){
IMachineStateChangedEvent=IMachineStateChangedEvent.queryInterface(rawEvent);
System.out.println(“机器”+event.getMachineId()+”状态更改为“+event.getState());
}
}最后{
管理器.getVBox().getEventSource().eventProcessed(el,rawEvent);
}
}
}捕获(可丢弃的t){
t、 printStackTrace();
}最后{
管理器.getVBox().getEventSource().unr