How to get java class in CF

2019-07-09 10:36发布

问题:

I want to access the return value from a java method in my coldfusion file. I have loaded all the jar files in coldfusion file and got the java class object successfully. Using the class object, I want to access java class method which returns a Set; but I can't get any return value. Here is my Java Code:

public Set getSession(String url) {       
    result+="hello";
    try {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);
        caps.setCapability("takesScreenshot", false);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                "E:\\TicketScraper\\phantomjs\\phantomjs.exe"
        );
        driver = new PhantomJSDriver(caps);
        driver.get(url);
        driver.findElement(By.id("login:loginName")).sendKeys("XXXX");
        driver.findElement(By.id("login:password")).sendKeys("XXXX");
        waitForJQueryProcessing(driver, 5);
        driver.findElement(By.id("login:j_idt145")).click();        
        Thread.sleep(10000);
        Set<org.openqa.selenium.Cookie> allCookies=driver.manage().getCookies();
        for ( org.openqa.selenium.Cookie loadedCookie : allCookies) {
            System.out.println(String.format("%s -> %s", loadedCookie.getName(),loadedCookie.getValue()));
        }      
    } catch(Exception e) {
        System.out.println(e);
    }
    return allCookies;
}

The java code runs the Phantom JS driver, logs in to the URL in the above code, and gets all cookies. All cookies are collected in a Set variable and returned from the method. I want to get this set variable in CF code.

But when I have tried to access the java method's Set variable in CF it doesn't return any value. By contrast, when I have commented out all the Phantom JS code and return only a String variable then CF can access the string value. Here is my CF code:

<cfscript>
    paths = arrayNew(1);
    paths[1] = expandPath("lib\apache-mime4j-0.6.jar");
    paths[2] = expandPath("lib\bsh-1.3.0.jar"); 
    paths[3] = expandPath("lib\cglib-nodep-2.1_3.jar");
    paths[4] = expandPath("lib\commons-codec-1.9.jar");
    paths[5] = expandPath("lib\commons-collections-3.2.1.jar");
    paths[6] = expandPath("lib\commons-exec-1.1.jar");
    paths[7] = expandPath("lib\commons-io-2.4.jar");
    paths[8] = expandPath("lib\commons-jxpath-1.3.jar");
    paths[9] = expandPath("lib\commons-lang3-3.3.2.jar");
    paths[10] = expandPath("lib\commons-logging-1.1.3.jar");
    paths[11] = expandPath("lib\Counsel_Cookies_Phantom.jar");
    paths[12] = expandPath("lib\cssparser-0.9.14.jar");
    paths[13] = expandPath("lib\gson-2.3.jar");
    paths[14] = expandPath("lib\guava-18.0.jar");
    paths[15] = expandPath("lib\hamcrest-core-1.3.jar");
    paths[16] = expandPath("lib\hamcrest-library-1.3.jar");
    paths[17] = expandPath("lib\htmlunit-2.15.jar");
    paths[18] = expandPath("lib\htmlunit-core-js-2.15.jar");
    paths[19] = expandPath("lib\httpclient-4.3.4.jar");
    paths[20] = expandPath("lib\httpcore-4.3.2.jar");
    paths[21] = expandPath("lib\httpmime-4.3.4.jar");
    paths[22] = expandPath("lib\ini4j-0.5.2.jar");
    paths[23] = expandPath("lib\jcommander-1.29.jar");
    paths[24] = expandPath("lib\jetty-websocket-8.1.8.jar");
    paths[25] = expandPath("lib\jna-3.4.0.jar");
    paths[26] = expandPath("lib\jna-platform-3.4.0.jar");
    paths[27] = expandPath("lib\junit-dep-4.11.jar");
    paths[28] = expandPath("lib\netty-3.5.7.Final.jar");
    paths[29] = expandPath("lib\nekohtml-1.9.21.jar");
    paths[30] = expandPath("lib\operadriver-1.5.jar");
    paths[31] = expandPath("lib\phantomjsdriver-1.1.0.jar");
    paths[32] = expandPath("lib\protobuf-java-2.4.1.jar");
    paths[33] = expandPath("lib\sac-1.3.jar");
    paths[34] = expandPath("lib\selenium-java-2.44.0.jar");
    paths[35] = expandPath("lib\selenium-java-2.44.0-srcs.jar");
    paths[36] = expandPath("lib\serializer-2.7.1.jar");
    paths[37] = expandPath("lib\testng-6.8.5.jar");
    paths[38] = expandPath("lib\xalan-2.7.1.jar");
    paths[39] = expandPath("lib\xercesImpl-2.11.0.jar");
    paths[40] = expandPath("lib\xml-apis-1.4.01.jar");
    paths[41] = expandPath("lib\Selenium_Cookies.jar");
    paths[42] = expandPath("lib\selenium-server-2.0b2.jar");
    //writeDump(paths);

    //create the loader
    loader = createObject("component", "javaloader.JavaLoader").init(paths,true);
    //writeDump(loader);

    excelObject = loader.create("counsel_cookies_phantom.Counsel_Cookies_Phantom");
    //writeDump(excelObject);
    //abort;
</cfscript>

<cfdump var=#excelObject.getSession("https://pacer.login.uscourts.gov/csologin/login.jsf")#/>
<cfabort>

Please provide your suggestions of how to access the Phantom JS value in CF.

回答1:

Assuming your Set is a java.util.Set, then calling toArray() would give you an array that's easily accessible in CF.

e.g.

<cfscript>
s = createObject("java", "java.util.HashSet").init();
s.add("foo");
s.add("bar");
s.add("bob");

arr = s.toArray();

writeDump(arr);
</cfscript>

Run this on TryCF.com