Page 1 of 1

How to use XPCOM bridge on Java?

Posted: 22. Jul 2011, 10:39
by cratle
Hi.

I'm trying to control VirtualBox with Java. so, I installed VirtualBox 4.1.0.
Host is Ubuntu 10.04.

And this is source code...

Code: Select all

import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VirtualBoxManager;


public class VirtualManager {
   public static void main(String[] args) {
	VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
	
	IVirtualBox vbox = mgr.getVBox();
	System.out.println(“VirtualBox version: “ + vbox.getVersion() + “\n”);

	// get first VM name
	String m = vbox.getMachines().get(0).getName();
	System.out.println(“\nAttempting to start VM ’“ + m + “’“);

	// start it
	mgr.startVm(m, null, 7000);
	mgr.cleanup();
   }
}
but it doesn't work. error Message is
Exception in thread "main" java.lang.RuntimeException: vbox.home Java property must be defined to use XPCOM bridge
at org.virtualbox_4_1.VirtualBoxManager.createInstance(VirtualBoxManager.java:101)
at VirtualManager.main(VirtualManager.java:22)

what's wrong? what I miss?

thanks for reading.....and answer

Re: How to use XPCOM bridge on Java?

Posted: 20. Jan 2012, 10:31
by zdvirtual
You need to set up the following argument to your IDE, e.g., eclipse:
-Dvbox.home=/usr/lib/virtualbox/

(replace the above location /usr/lib/virtualbox with your own lib is located, in linux type in terminal: whereis virtualbox

Steps to set up the argument to eclipse:
1) Right click on your project folder
2) Select "Run As" > "Run Configurations..."
3) Click on the "(x)=Arguments" tab > Under the "VM arguments" type in the following: -Dvbox.home=/usr/lib/virtualbox/

Also you may find the following useful:
Oracle VM VirtualBox Programming Guide and Reference, Chapter 10 - Using Java API page 295:

Below I have included an extract:

[XPCOM: - for all platforms, but Microsoft Windows. A Java bridge based on JavaXPCOM is
shipped with VirtualBox. The classpath must contain vboxjxpcom.jar and the vbox.home
property must be set to location where the VirtualBox binaries are. Please make sure that
the JVM bitness matches bitness of VirtualBox you use as the XPCOM bridge relies on native
libraries.
Start your application like this:
java -cp vboxjxpcom.jar -Dvbox.home=/opt/virtualbox MyProgram]

Re: How to use XPCOM bridge on Java?

Posted: 21. Feb 2019, 12:01
by Atiq
I am using window host os and my java code is in eclipse to access the VirtualBox and I have set the path of vm argument in eclipse where i have install the virtualbox "D:\FinalYear\FYP\SetupVirtualBx\installDirectoryVirtualbox" but still, it is not working. Can you please share the screenshot of the path set to the eclipse for the window. I will be grateful if you can send me this information.
And can you please tell me that what is this path " -Dvbox.home=/usr/lib/virtualbox/". Is it a installed directory of virtualbox or anything else. How can i set the path in window host.

Re: How to use XPCOM bridge on Java?

Posted: 21. Feb 2019, 12:14
by socratis
I highly doubt that you're going to get an answer...
"zdvirtual" last logged in on 2012-01-26, 04:29 and "cratle" (the OP) last logged in on 2011-07-25, 03:44. :shock:

I'm moving this to the "VirtualBox API" from the "Linux Hosts" area, you might have a better chance of getting a response there...

Re: How to use XPCOM bridge on Java?

Posted: 11. Jul 2019, 13:13
by noteirak
A bit of a late answer, but XPCOM has always been a difficult topic. In Hyperbox, I have made this a bit less painless.

This is how we get a VBoxManager object: https://github.com/hyperbox/vbox-5.1/bl ... r.java#L48
And this is the VBoxXPCOM class: https://github.com/hyperbox/vbox-common ... M.java#L30

So basically, it boils down to knowing where VirtualBox is installed, in this case we assume /usr/lib/virtualbox and then calling the constructor with it, instead of using properties. The SDK Jar only needs to be in the classpath.

Code: Select all

VirtualBoxManager.createInstance("/usr/lib/virtualbox");
There are bigger concerns with using XPCOM, which are given into comments in the two files.

Re: How to use XPCOM bridge on Java?

Posted: 11. Jul 2019, 14:34
by mpack
noteirak wrote:In Hyperbox, I have made this a bit less painless.
Ironic unintended double negative? :)

Re: How to use XPCOM bridge on Java?

Posted: 11. Jul 2019, 14:36
by noteirak
I blame speaking more French recently: my english sure took a hit :(