How to use XPCOM bridge on Java?

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
cratle
Posts: 1
Joined: 22. Jul 2011, 10:28
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: not yet

How to use XPCOM bridge on Java?

Post 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
zdvirtual
Posts: 1
Joined: 20. Jan 2012, 10:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: Windows Vista

Re: How to use XPCOM bridge on Java?

Post 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]
Atiq
Posts: 1
Joined: 15. Feb 2019, 09:22

Re: How to use XPCOM bridge on Java?

Post 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.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to use XPCOM bridge on Java?

Post 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...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to use XPCOM bridge on Java?

Post 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.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
mpack
Site Moderator
Posts: 39156
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to use XPCOM bridge on Java?

Post by mpack »

noteirak wrote:In Hyperbox, I have made this a bit less painless.
Ironic unintended double negative? :)
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to use XPCOM bridge on Java?

Post by noteirak »

I blame speaking more French recently: my english sure took a hit :(
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply