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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With