LiveConnect unable to initialise certain classes in firefox extension java.lang.reflect.InvocationTargetException

StackOverflow https://stackoverflow.com/questions/10779345

Question

I followed this guide https://developer.mozilla.org/en/Java_in_Firefox_Extensions to enable me to access java classes in a firefox plugin (I am actually adding them to a user-extension for selenium ide).

Everything works great with the guide above, until my java code tries to initialise imported objects...

So if I import jtidy to try to clean the html, as soon as I initialise it I get a java.lang.reflect.InvocationTargetException exception. Interestingly I can initialise the jtidy class in the javascript and then pass this through and it works until I try to parse the dom. I think this all has to do with permissions, but I haven't worked enough with Live Connect to know where to go from here.

My code is as follows:

Javascript

PageBot.prototype.locateElementByLabelText = function(labelText, inDocument) {
    //var pageTitle = inDocument.getElementsByTagName('title').item(0).nodeName;
    var pageTitle = inDocument.title;
    LOG.warn("Title = "+ pageTitle);
    var pageSource = inDocument.getElementsByTagName('html')[0].innerHTML;
    LOG.warn("I have the page source");
    var xpathValue = getXpathUsingJavaFnc(labelText, pageTitle, pageSource);
    LOG.warn("Xpath = "+xpathValue);
    return this.locateElementByXPath(xpathValue, inDocument);
};


Selenium.prototype.doLoadProject = function(locator, paramString) {
    LOG.warn("starting to load Load Project "+locator + " " + paramString);
    try{
    var result = inst.initialiseProject(locator, Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('AUTOMATED_HOME')+"/");
    }catch(e){
    LOG.warn(e.message);
    }
    LOG.warn("finished Load Project");
    LOG.warn(result);
};




var basePath = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment).get('AUTOMATED_HOME');
 var fullPath = 'file:///' + basePath + '/ScenarioFramework-jar-with-dependencies.jar';

LOG.warn("full path = "+fullPath);
var extensionUrl = "file:///" + basePath +'/javaFirefoxExtensionUtils.jar';  
var classLoaderJarpath = extensionUrl;  
var myJarpath = fullPath;   
//var testTidy = "file:///" + basePath +'/jtidy-r938.jar'; 

var urlArray = []; 
urlArray[0] = new java.net.URL(myJarpath);  
urlArray[1] = new java.net.URL(classLoaderJarpath);  
urlArray[2] = new java.net.URL(testTidy); 
var cl = java.net.URLClassLoader.newInstance(urlArray);  

policyAdd(cl, urlArray);  
var classToLoad = "scenario.framework.main.SeleniumRunner";
var aClass = cl.loadClass(classToLoad);
//var aClass = java.lang.Class.forName(classToLoad, true, cl);
var inst = aClass.newInstance();

LOG.warn("finished loading add Java");



function getXpathUsingJavaFnc(eleName, pageTitle, pageSource) {
LOG.warn("Testing getXpath");
    try{    
        var xpathValue = inst.findElementUsingName(eleName, pageTitle, pageSource);

    }catch(e){
        //var jvm = Components.classes["@mozilla.org/oji/jvm-mgr;1"].getService(Components.interfaces.nsIJVMManager);
        //jvm.showJavaConsole();
        logExc(e);
    }
return xpathValue;
};


function policyAdd (loader, urls) {  
    try {       
        var str = 'edu.mit.simile.javaFirefoxExtensionUtils.URLSetPolicy';  
        var policyClass = java.lang.Class.forName(  
           str,  
           true,  
           loader  
    );  
    var policy = policyClass.newInstance();  
    policy.setOuterPolicy(java.security.Policy.getPolicy());  
    java.security.Policy.setPolicy(policy);  
    policy.addPermission(new java.security.AllPermission());  
    for (var j=0; j < urls.length; j++) {  
        policy.addURL(urls[j]);  
    }  
    java.lang.System.setSecurityManager(null);

var t = java.lang.Thread.currentThread().setContextClassLoader(cl);

    }catch(e) {  
        logExc(e);
    }  
}  


function logExc(e) {
    try {
        while(e != null) {
            LOG.warn(e);
            e = e.getCause();
        }
        LOG.warn(netscape.javascript.JSUtil.getStackTrace(e));
    } catch(exp) {
         LOG.warn("exception while logging exception");
         LOG.warn(exp);
        LOG.warn("exception being logged");
        LOG.warn(e);
    }
}

Java:

public static String findElementUsingName(String Name, String pageTitle, String html){
     org.w3c.tidy.Tidy tidy = new org.w3c.tidy.Tidy();
     return "//input[@id='password']";
}
Was it helpful?

Solution

Restart Firefox after each compile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top