Download Gecko SDK aka XULRunner SDK from Mozilla. After installation, run
xulrunner.exe --register-user
Add xulrunner-sdk\bin to your PATH variable. If the path variable is not set, you would see the following exception
Exception in thread “main” java.lang.UnsatisfiedLinkError: C:\xulrunner\javaxpcomglue.dll: Can’t find dependent libraries
Now we create a new java eclipse project. Add MozillaInterfaces.jar and MozillaGlue.jar from xulrunner-sdk\lib\ to java build path. Write a simple class for driving the browser. Here is full class code
import java.io.File;
import org.mozilla.interfaces.nsIAppStartup;
import org.mozilla.interfaces.nsIDOMWindow;
import org.mozilla.interfaces.nsIServiceManager;
import org.mozilla.interfaces.nsIWindowCreator;
import org.mozilla.interfaces.nsIWindowWatcher;
import org.mozilla.xpcom.Mozilla;
public class TestClass {
public static void main(String[] args) throws Exception {
Mozilla mozilla = Mozilla.getInstance();
File grePath = new File("D:/softwares/xulrunner-sdk/bin");
LocationProvider locProvider = new LocationProvider(grePath);
mozilla.initialize(grePath);
mozilla.initEmbedding(grePath, grePath, locProvider);
// Now we need to start an XUL application, so we get an instance of the
// XPCOM service manager
nsIServiceManager serviceManager = mozilla.getServiceManager();
// Now we need to get the @mozilla.org/toolkit/app-startup;1 service:
nsIAppStartup appStartup = (nsIAppStartup) serviceManager
.getServiceByContractID("@mozilla.org/toolkit/app-startup;1",
nsIAppStartup.NS_IAPPSTARTUP_IID);
// Get the nsIWindowWatcher interface to the above
nsIWindowCreator windowCreator = (nsIWindowCreator) appStartup
.queryInterface(nsIWindowCreator.NS_IWINDOWCREATOR_IID);
// Get the window watcher service
nsIWindowWatcher windowWatcher = (nsIWindowWatcher) serviceManager
.getServiceByContractID(
"@mozilla.org/embedcomp/window-watcher;1",
nsIWindowWatcher.NS_IWINDOWWATCHER_IID);
// Set the window creator (from step 6)
windowWatcher.setWindowCreator(windowCreator);
// Create the root XUL window:
nsIDOMWindow win = windowWatcher.openWindow(null,
"http://www.google.com", "mywindow",
"chrome,resizable,centerscreen", null);
// Set this as the active window
windowWatcher.setActiveWindow(win);
// Hand over the application to xpcom/xul, this will block:
appStartup.run();
mozilla.termEmbedding();
}
}
This uses LocationProvider class, which is more or less standard unless you want to play with package structure and is presented next
import java.io.File;
import org.mozilla.xpcom.IAppFileLocProvider;
public class LocationProvider implements IAppFileLocProvider {
File grePath;
public LocationProvider(File grePath) {
this.grePath = grePath;
}
public File getFile(String aProp, boolean[] aPersistent) {
File file = null;
if (aProp.equals("GreD") || aProp.equals("GreComsD")) {
file = grePath;
if (aProp.equals("GreComsD")) {
file = new File(file, "components");
}
} else if (aProp.equals("MozBinD") || aProp.equals("CurProcD")
|| aProp.equals("ComsD") || aProp.equals("ProfD")) {
file = grePath;
if (aProp.equals("ComsD")) {
file = new File(file, "components");
}
}
return file;
}
public File[] getFiles(String aProp) {
//System.out.println(aProp);
File[] files = null;
if (aProp.equals("APluginsDL")) {
files = new File[1];
files[0] = new File(grePath, "plugins");
}
return files;
}
}