11

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.

1
  • Did you find a fix @TrentCoder?
    – aandis
    Feb 2, 2015 at 13:25

2 Answers 2

1

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.

1

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)

2
  • Do you mean GuestSessionWaitForFlag_Start rather than 1L?
    – Paul Hicks
    May 28, 2014 at 22:38
  • Yes. I have the same job and process starts when my app waits for the guest session is in started status. When I comment waitFor (or waitForArray) guest session state will be in Error. Possibly, you have to try different Flags. For instance, flag GuestSessionWaitForFlag_Status (or 4L) in order to wait until the session status is changed May 29, 2014 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.