2012-12-12 97 views
8

我想編寫一個Java程序,在VirtualBox客機中啓動一個程序(例如:Firefox)。主機是Windows,客戶是Ubuntu。根據SDK文檔,應該可以做到這一點。這是我在Java中嘗試基於我所見過的文檔中(假設虛擬機運行起來):VirtualBox:使用SDK API在guest虛擬機中啓動程序?

String machineName = "MyMachine"; 
String url = "http://localhost:18083"; 
String user = ""; 
String passwd = ""; 

VirtualBoxManager mgr = VirtualBoxManager.createInstance(null); 
mgr.connect(url, user, passwd); 
IVirtualBox vbox = mgr.getVBox(); 
System.out.println("Running VirtualBox version " + vbox.getVersion()); 

IMachine machine = vbox.findMachine(machineName); 
ISession session = mgr.getSessionObject(); 
machine.lockMachine(session, LockType.Shared); 
IConsole console = session.getConsole(); 
IGuest guest = console.getGuest(); 

IGuestSession guestSession = guest.createSession("bob","password", "", ""); 
guestSession.processCreate("/usr/bin/firefox", null, null, null, 0L); 

session.unlockMachine(); 

當我跑,我得到:

Exception in thread "main" org.virtualbox_4_2.VBoxException: VirtualBox error: The session is not locked (session state: Unlocked) (0x8000FFFF) 
at org.virtualbox_4_2.ISession.getConsole(ISession.java:145) 
at Test1.main(Test1.java:27) 

我對鎖和會話嘗試了不同的選項,但總是遇到某種類型的錯誤。如果我將LockType設置爲Write,則會出現「將計算機分配給會話失敗」錯誤。

有沒有人這樣做?有沒有可靠的Java VirtualBox教程在線?我找不到Google。

任何意見讚賞。

+0

你找到一個解決@TrentCoder? – aandis

回答

0

我也不知道如何用SDK做到這一點。但是你有沒有試過通過SSH向虛擬機發送推薦?因此,您需要設置網絡以及所有這些,但這將是一個很好的選擇,因爲缺少VirtualBox SDK文檔會導致存檔變得複雜。

0

您是否嘗試等待客人會話開始。你的情況這應該是這樣的

guestSession = guest.CreateSession(....

guestSession.waitFor(1L,0L)

+0

你的意思是'GuestSessionWaitForFlag_Start'而不是'1L'嗎? –

+0

是的,當我的應用程序等待客戶端會話處於啓動狀態時,我有相同的工作和進程啓動。當我評論waitFor(或waitForArray)來賓會話狀態時in Error。可能的話,你必須嘗試不同的標誌,例如,標誌GuestSessionWaitForFlag_Status (或4L),以便等待會話狀態改變 – Sergey

相關問題