java中vbox是什么_java_virtualbox

本文提供了一个使用Java编写的VirtualBox API示例程序,演示了如何通过同一源代码连接到Web服务和XP COM API,包括枚举虚拟机、读取日志、启动虚拟机等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* $Id: TestVBox.java 101270 2015-06-25 11:31:10Z klaus $ *//* Small sample/testcase which demonstrates that the same source code can* be used to connect to the webservice and (XP)COM APIs. *//** Copyright (C) 2010-2015 Oracle Corporation** This file is part of VirtualBox Open Source Edition (OSE), as* available from http://www.virtualbox.org. This file is free software;* you can redistribute it and/or modify it under the terms of the GNU* General Public License (GPL) as published by the Free Software* Foundation, in version 2 as it comes in the "COPYING" file of the* VirtualBox OSE distribution. VirtualBox OSE is distributed in the* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.*/importorg.virtualbox_5_0.*;importjavax.swing.*;importjava.util.List;importjava.util.Arrays;//import java.math.BigInteger;public classTestVBoxFive

{

private static voidprocessEvent(IEvent ev)

{

System.out.println("got event: "+ ev);VBoxEventType type = ev.getType();System.out.println("type = "+ type);switch(type)

{

caseOnMachineStateChanged:

{

IMachineStateChangedEvent mcse = IMachineStateChangedEvent.queryInterface(ev);if(mcse == null)

System.out.println("Cannot query an interface");elseSystem.out.println("mid="+ mcse.getMachineId());break;}

}

}

static classEventHandler

{

EventHandler() {}

public voidhandleEvent(IEvent ev)

{

try{

processEvent(ev);} catch(Throwable t) {

t.printStackTrace();}

}

}

private static voidtestEvents(VirtualBoxManager mgr,IEventSource es)

{

// active mode for Java doesn't fully work yet, and using passive// is more portable (the only mode for MSCOM and WS) and thus generally// recommendedIEventListener listener = es.createListener();es.registerListener(listener,Arrays.asList(VBoxEventType.Any), false);try{

for(inti = 0;i < 50;i++)

{

System.out.print(".");IEvent ev = es.getEvent(listener,500);if(ev != null)

{

processEvent(ev);es.eventProcessed(listener,ev);}

// process system event queuemgr.waitForEvents(0);}

} catch(Exception e) {

e.printStackTrace();}

es.unregisterListener(listener);}

private static voidtestEnumeration(VirtualBoxManager mgr,IVirtualBox vbox)

{

List machs = vbox.getMachines();for(IMachine m : machs)

{

String name;Long ram = 0L;booleanhwvirtEnabled = false,hwvirtNestedPaging = false;booleanpaeEnabled = false;booleaninaccessible = false;try{

name = m.getName();ram = m.getMemorySize();hwvirtEnabled = m.getHWVirtExProperty(HWVirtExPropertyType.Enabled);hwvirtNestedPaging = m.getHWVirtExProperty(HWVirtExPropertyType.NestedPaging);paeEnabled = m.getCPUProperty(CPUPropertyType.PAE);// String osType = m.getOSTypeId();// IGuestOSType foo = vbox.getGuestOSType(osType);}

catch(VBoxException e)

{

name = "";inaccessible = true;}

System.out.println("VM name: "+ name);if(!inaccessible)

{

System.out.println(" RAM size: "+ ram + "MB"+ ", HWVirt: "+ hwvirtEnabled

+ ", Nested Paging: "+ hwvirtNestedPaging

+ ", PAE: "+ paeEnabled);}

}

// process system event queuemgr.waitForEvents(0);}

private static booleanprogressBar(VirtualBoxManager mgr,IProgress p, longwaitMillis)

{

longend = System.currentTimeMillis() + waitMillis;while(!p.getCompleted())

{

// process system event queuemgr.waitForEvents(0);// wait for completion of the task, but at most 200 msecsp.waitForCompletion(200);if(System.currentTimeMillis() >= end)

return false;}

return true;}

private static voidtestStart(VirtualBoxManager mgr,IVirtualBox vbox)

{

IMachine m = vbox.getMachines().get(0);String name = m.getName();System.out.println("\nAttempting to start VM '"+ name + "'");if(name.contains("Android")){

System.out.println("------------------------------");ISession session = mgr.getSessionObject();IProgress p = m.launchVMProcess(session,"gui","");progressBar(mgr,p,10000);session.unlockMachine();// process system event queuemgr.waitForEvents(0);}

}

private static voidgetDispalyInfo(VirtualBoxManager mgr,IVirtualBox vbox){

System.out.println("\nvbox Dispaly Info:");longscreenId = 0;IMachine m = vbox.getMachines().get(0);ISession session = mgr.getSessionObject();m.lockMachine(session,LockType.VM);IConsole console = session.getConsole();IProgress p = console.powerUpPaused();progressBar(mgr,p,10000);IDisplay display = console.getDisplay();MainView mainView = newMainView();IFramebuffer framebuffer = newIFramebuffer(mainView);// framebuffer.notifyUpdate(100L, 100L, 20L, 20L);display.attachFramebuffer(screenId,framebuffer);IFramebuffer framebufferOut = display.queryFramebuffer(screenId);System.out.println("framebuffer:"+ framebufferOut);bytevboxAddress = 0;display.getTypedWrapped().drawToScreen(screenId,vboxAddress,100L,100L,20L,20L);// Holder w = new Holder<>();// Holder h = new Holder<>();// Holder pix = new Holder<>();// Holder x = new Holder<>();// Holder y = new Holder<>();// Holder status = new Holder<>();// display.getScreenResolution(screenId, w, h, pix, x, y, status);//// session.unlockMachine();//// System.out.print("w:" + w.value + " h:" + h.value + " pix:" + pix.value + " x:" + x.value + " y:" + y.value);// testStart(mgr, vbox);}

static voidtestMultiServer()

{

VirtualBoxManager mgr1 = VirtualBoxManager.createInstance(null);VirtualBoxManager mgr2 = VirtualBoxManager.createInstance(null);try{

mgr1.connect("http://i7:18083","","");mgr2.connect("http://main:18083","","");IMachine m1 = mgr1.getVBox().getMachines().get(0);IMachine m2 = mgr2.getVBox().getMachines().get(0);// String name1 = m1.getName();// String name2 = m2.getName();ISession session1 = mgr1.getSessionObject();ISession session2 = mgr2.getSessionObject();IProgress p1 = m1.launchVMProcess(session1,"gui","");IProgress p2 = m2.launchVMProcess(session2,"gui","");progressBar(mgr1,p1,10000);progressBar(mgr2,p2,10000);session1.unlockMachine();session2.unlockMachine();// process system event queuemgr1.waitForEvents(0);mgr2.waitForEvents(0);} finally{

mgr1.cleanup();mgr2.cleanup();}

}

private static voidtestReadLog(VirtualBoxManager mgr,IVirtualBox vbox)

{

IMachine m = vbox.getMachines().get(0);longlogNo = 0;longoff = 0;longsize = 16* 1024;while(true)

{

byte[] buf = m.readLog(logNo,off,size);if(buf.length== 0)

break;System.out.print(newString(buf));off += buf.length;}

// process system event queuemgr.waitForEvents(0);}

private static voidprintErrorInfo(VBoxException e)

{

System.out.println("VBox error: "+ e.getMessage());System.out.println("Error cause message: "+ e.getCause());System.out.println("Overall result code: "+ Integer.toHexString(e.getResultCode()));inti = 1;for(IVirtualBoxErrorInfo ei = e.getVirtualBoxErrorInfo();ei != null;ei = ei.getNext(),i++)

{

System.out.println("Detail information #"+ i);System.out.println("Error mesage: "+ ei.getText());System.out.println("Result code: "+ Integer.toHexString(ei.getResultCode()));// optional, usually provides little additional information:System.out.println("Component: "+ ei.getComponent());System.out.println("Interface ID: "+ ei.getInterfaceID());}

}

private static voidshowMainView(){

JFrame frame = newJFrame("MainForm");JPanel panel_main = newMainForm().panel_main;frame.setContentPane(panel_main);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.pack();frame.setVisible(true);}

public static voidmain(String[] args)

{

showMainView();// System.out.println("java.library.path: " + System.getProperties().get("java.library.path") + "\n");VirtualBoxManager mgr = VirtualBoxManager.createInstance("/Applications/VirtualBox.app/Contents/MacOS");// VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);booleanws = false;String url = null;String user = null;String passwd = null;for(inti = 0;i < args.length;i++)

{

if(args[i].equals("-w"))

ws = true;else if(args[i].equals("-url"))

url = args[++i];else if(args[i].equals("-user"))

user = args[++i];else if(args[i].equals("-passwd"))

passwd = args[++i];}

if(ws)

{

try{

mgr.connect(url,user,passwd);} catch(VBoxException e) {

e.printStackTrace();System.out.println("Cannot connect, start webserver first!");}

}

try{

IVirtualBox vbox = mgr.getVBox();if(vbox != null)

{

System.out.println("VirtualBox version: "+ vbox.getVersion() + "\n");testEnumeration(mgr,vbox);testReadLog(mgr,vbox);// getDispalyInfo(mgr, vbox);testStart(mgr,vbox);// testEvents(mgr, vbox.getEventSource());//// System.out.println("done, press Enter...");// int ch = System.in.read();}

}

catch(VBoxException e)

{

printErrorInfo(e);System.out.println("Java stack trace:");e.printStackTrace();}

catch(RuntimeException e)

{

System.out.println("Runtime error: "+ e.getMessage());e.printStackTrace();}

// catch (java.io.IOException e)// {// e.printStackTrace();// }// process system event queuemgr.waitForEvents(0);if(ws)

{

try{

mgr.disconnect();} catch(VBoxException e) {

e.printStackTrace();}

}

mgr.cleanup();}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值