Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VirtualBox: Using the SDK API to launch a program in the guest?

I would like to write a Java program that launches a program (ex: Firefox) in a VirtualBox guest machine. The host is Windows and the guest is Ubuntu. According to the SDK documentation, it should be possible to do this. Here is my attempt in Java based on what I've seen in the documentation (this assumes the VM is up and running):

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();

When I run this, I get:

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)

I've tried different options with the locks and sessions, but always get some type of error. If I set the LockType to Write, I get "Failed to assign the machine to the session" error.

Has anyone done this? Are there any solid Java VirtualBox tutorials online? I can't find any with Google.

Any advice appreciated.

like image 337
TrentCoder Avatar asked Dec 12 '12 06:12

TrentCoder


People also ask

Does VirtualBox have an API?

The VirtualBox Main API (also called the VirtualBox COM library) contains two public component classes: VirtualBox. VirtualBox and VirtualBox. Session , which implement IVirtualBox and ISession interfaces respectively.

Do I need to install VirtualBox guest additions?

After installing Windows 10 in a VirtualBox VM, you must install the Guest Additions package for better usability and performance – here's how. On VirtualBox, the “Guest Additions” package contains the drivers that allow the Windows 10 virtual machine to operate correctly in a virtualization environment.


2 Answers

I also don't know how to do it with the SDK. But have you tried to send the commend via SSH to the virtual machine? Sue you need to set up the network and all that, but it would be a good alternative, since the lack of VirtualBox SDK documentation makes it complicated to archive.

like image 200
Kenyakorn Ketsombut Avatar answered Sep 19 '22 15:09

Kenyakorn Ketsombut


Have you tried to wait until guest session is started. In your case this should be something like this

guestSession = guest.CreateSession(....

guestSession.waitFor(1L, 0L)

like image 38
Sergey Ivanskoy Avatar answered Sep 20 '22 15:09

Sergey Ivanskoy