如何在CF中获取java类 [英] How to get java class in CF
问题描述
我想从我的coldfusion文件中的java方法访问返回值。我已经加载所有的jar文件在coldfusion文件,并获得了java类对象成功。使用类对象,我想访问java类方法,它返回一个 Set
;但我不能得到任何返回值。
这是我的Java代码:
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;
}
java代码运行Phantom JS驱动程序,登录到上面的代码,并获取所有的cookie。所有cookie都收集在 Set
变量中,并从方法中返回。我想在CF代码中获得这个设置变量。
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.
但是当我试图访问java方法的 Set
在CF中的变量不返回任何值。相比之下,当我注释掉所有Phantom JS代码并只返回一个 String
变量,然后CF可以访问字符串值。
这是我的CF代码:
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>
请提供您如何访问CF中Phantom JS值的建议。
Please provide your suggestions of how to access the Phantom JS value in CF.
推荐答案
假设你的 Set
是一个 java.util.Set
,然后调用
toArray()
会给你一个容易在CF中访问的数组。
Assuming your Set
is a java.util.Set
, then calling toArray()
would give you an array that's easily accessible in CF.
例如
<cfscript>
s = createObject("java", "java.util.HashSet").init();
s.add("foo");
s.add("bar");
s.add("bob");
arr = s.toArray();
writeDump(arr);
</cfscript>
这篇关于如何在CF中获取java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!