Hey all,
I'm brand new to Java and JavaLoader so some of what I'm describing
may seem crazy, or just a very bad way of doing it.
I'm trying to run some Java code from within CF that drives WebDriver.
I have a Java project in Eclipse that "works". It contains a /lib
folder with a whole bunch of jar files that I downloaded from
http://code.google.com/p/selenium/ (specifically
selenium-java-2.0a4.zip). In Eclipse I added the jars to my buildpath
and that allowed me to run my code. I have a single java file in the
/src folder that does some simple stuff with WebDriver. Here's the
source of that:
Example.java:
import
org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
FirefoxDriver driver;
WebElement e;
public Example() {
}
public void tryIt() {
driver = new FirefoxDriver();
System.setProperty("webdriver.firefox.useExisting","true");
driver.get("
http://www.google.com");
e = driver.findElement(By.name("q"));
e.sendKeys("Cheese!");
e.submit();
System.out.println("Page title is " + driver.getTitle());
driver.quit();
}
}
Being totally new to Java and JavaLoader, I just tried loading my
source file initially, using the sourceDirectories argument, but of
course Java couldn't find the org.openqa.selenium package. I then
tried loading my /lib folder into JavaLoader using the loadPaths
argument, but that didn't seem to work.
Question 1: Can I just point loadPaths at a folder on disk that
contains a bunch of jar files, or do I have to point it to each
individual jar file? Doing the former didn't seem to work.
I wasn't sure how to proceed at this point, so what I did was use the
Export feature of Eclipse to create a jar file that includes all of
the resources in my /lib folder. I then pointed to that jar file
using loadPaths. I have no idea if that's a reasonable approach and
would be happy to hear of other ways to make this work.
OK, so now the Java source is running and it is able to find the
org.openqa.selenium package, but when I run the code I get the
following exception:
org.openqa.selenium.WebDriverException: java.lang.ClassCastException:
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to
javax.xml.parsers.DocumentBuilderFactory
System info:
os.name: 'Mac OS X', os.arch: 'x86_64', os.version:
'10.6.3', java.version: '1.6.0_17'
Driver info: driver.version: firefox
at org.openqa.selenium.firefox.FirefoxProfile.readIdFromInstallRdf(FirefoxProfile.java:224)
at org.openqa.selenium.firefox.FirefoxProfile.addExtension(FirefoxProfile.java:160)
at org.openqa.selenium.firefox.FirefoxProfile.addExtension(FirefoxProfile.java:142)
at org.openqa.selenium.firefox.FirefoxProfile.addWebDriverExtensionIfNeeded(FirefoxProfile.java:102)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:110)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:64)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:100)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:92)
at Example.tryIt(Example.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at coldfusion.runtime.StructBean.invoke(StructBean.java:502)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2393)
at cfindex2ecfm43082552.runPage(/Users/robertsilverberg/Documents/workspace/javaloader/example/compileHelloWorld/index.cfm:34)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:231)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:416)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:363)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:87)
at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:74)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:53)
at coldfusion.CfmServlet.service(CfmServlet.java:200)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
Caused by: java.lang.ClassCastException:
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to
javax.xml.parsers.DocumentBuilderFactory
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
at org.openqa.selenium.firefox.FirefoxProfile.readIdFromInstallRdf(FirefoxProfile.java:177)
... 45 more
I'm not sure what this is, but my guess is that it's some sort of
conflict between the code in my jar file and CF's java classes. Any
advice that anyone has on getting past this would be appreciated.
Oh, and here's the CF code that runs all of this:
<cfscript>
paths[1] = expandPath("./lib/cfTest.jar");
sourcePaths = [expandPath("./src")];
loader = createObject("component",
"javaloader.JavaLoader").init(loadPaths=paths,sourceDirectories=sourcePaths);
example = loader.create("Example").init();
example.tryIt();
</cfscript>
Thanks for any help with this!
Cheers,
Bob
--
Bob Silverberg
www.silverwareconsulting.com
Hands-on ColdFusion ORM Training
www.ColdFusionOrmTraining.com