XPCOMException: getServiceByContractID gates simple JavaXPCOM test

John Poole <[email protected]> Thu, 26 Jun 2008 14:51:50 -0700 (PDT)
Newsgroups gmane.comp.mozilla.devel.java
Message-ID <[email protected]>
I am unsuccessful in running a simple test of JavaXPCOM as I 
get the error:

 grePath = D:\Downloads\Mozilla\XUL\xulrunner
org.mozilla.xpcom.XPCOMException: The function "getServiceByContractID" returned an error condition  (0x80004002)
    at org.mozilla.xpcom.internal.XPCOMJavaProxy.callXPCOMMethod(Native Method)
    at org.mozilla.xpcom.internal.XPCOMJavaProxy.invoke(XPCOMJavaProxy.java:143)
    at $Proxy0.getServiceByContractID(Unknown Source)
    at com.oracle.appsfdoc.fusion.SimpleLauncher.main(SimpleLauncher.java:87)
    
In a previous post to this forum, "Having trouble with Embedding Mozilla"
dated Jul 9 2007, code was posted.  I've modified it slightly and am
posting it here as modified -- hopefully as a step forward 
and something that someone like me, wanting to try out this 
technology, could use without having to
do any further tweaking or configuration.

 I'm working in Eclipse and I'm using the following line (all one line)
 in my "VM arguments" to the Arguments
 -classpath .;'"D:\Downloads\Mozilla\XUL\xulrunner\javaxpcom.jar":"D:\Downloads\Mozilla\XUL\xulrunner-sdk\lib\MozillaInterfaces.jar":"D:\Downloads\Mozilla\XUL\xulrunner-sdk\lib\MozillaGlue.jar"
 
 I'm certain I'm probably not pointing to the correct file or something during
 run time as the code compiles, it's a run time error.
vvvvvvvvvvvvvvvvvvvvvvvvv
/*
 * Test script to launch xulrunner from java
 * 
 * requires registration of xulrunner, sample:
 *     D:\Downloads\Mozilla\XUL\xulrunner>xulrunner --register-global
 * 
 * requires jars:
 *     javaxpcom.jar
 *  from the sdk:
 *          MozillaGlue.jar
 *          MozillaInterfaces.jar
 *          [MozillaInterfaces-src.jar]
 */
package ###

import java.io.File;
import java.io.FileNotFoundException;

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.GREVersionRange;
import org.mozilla.xpcom.IAppFileLocProvider;
import org.mozilla.xpcom.Mozilla;
import org.mozilla.xpcom.XPCOMException;

public class SimpleLauncher {

    public static class LocationProvider implements IAppFileLocProvider {

        File grePath;

        public LocationProvider(File grePath) {
            this.grePath = grePath;
        }

        public File getFile(String aProp, boolean[] aPersistent) {
            // System.out.println(aProp);
            // System.out.println(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;
        }
    }

    public static void main(String[] args) {

        Mozilla mozilla = Mozilla.getInstance();
        GREVersionRange[] range = new GREVersionRange[1];
        range[0] = new GREVersionRange("1.8.0", true, "1.9", false);
        // work with trunk nightly version 1.9a1

        try {
            File grePath = Mozilla.getGREPathWithProperties(range, null);
            System.out.println("grePath = " + grePath.toString());

            mozilla.initialize(grePath);
            LocationProvider locProvider = new LocationProvider(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,
             * "chrome://your-app/content/window.xul", "mywindow",
             * "chrome,resizable,centerscreen", null);
             */
            String app = "chrome://your-app/content/window.xul";
            app = "chrome://D:\\work\\XUL\\test1\\application.ini";
            // Create the root XUL window:
            nsIDOMWindow win = windowWatcher.openWindow(null, app, "mywindow",
                    "chrome,resizable,centerscreen", null);

            // Set this as the active window
            windowWatcher.setActiveWindow(win);

            System.out.println("running...");

            // Hand over the application to xpcom/xul, this will block:
            appStartup.run();
            System.out.println("run completed.");
        } catch (FileNotFoundException e) {
            // this exception is thrown if greGREPathWithProperties cannot find
            // a GRE
            e.printStackTrace();
        } catch (XPCOMException e) {
            // this exception is thrown if initEmbedding failed
            e.printStackTrace();
        }
        // JavaXPOM finalization
        mozilla.shutdownXPCOM(null);
    }
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If someone helps me get past this road block, I'll be pleased to
reply with a complete ready-to-run code, if modified from above, with
all the dependencies spelled out.

Thank you,

John