Greg's Blog

helping me remember what I figure out

An Alternative to Loading JAR Files

| Comments

Here is an option to loading JAR files. Rather than placing them into the ClassPath of the server. Phil Cruz posted to the BD mailing list about his experience trying to integrate Log4J with BD (and since he can’t load it into the Classpath either I am assuming that he is using BD FREE as well). He came across a post from Spike that shows how to load a java class from a relative path. However Phill encountered an error when using this method. The method relies on creating a Java array and adding a URL object to it. This URL object is the path to the Java file that should be loaded. In CFMX this technique works, but in BD it returns a ”Argument is not an array” error. It has been logged as a bug. Andrwe Wu provided a simple work around to this problem (while the bug is being fixed). Instead of using a Java Array simply use a cfml one. So the code for loading log4J would be like follows: <cfset pathToLog4j = “C:\CFusionMX\lib\log4j.jar” /> <!— Create an instance of java.net.URL for passing to the URLClassLoader —> <cfset URLObject = createObject(‘java’,’java.net.URL’) /> <!— Initialize the object with the jar file —> <cfset URLObject.init(“file:” & pathToLog4j) /> <!— Create an Array and add our URLObject to it —> <cfset arr[1] = urlobject /> <!— Create and the URLClassLoader and pass it the array containing our path —> <cfset loader = createObject(‘java’,’java.net.URLClassLoader’) /> <cfset loader.init(arr) /> <!— Use our new class loader to load the DOMConfigurator class —> <cfset configurator = loader.loadClass(“org.apache.log4j.xml.DOMConfigurator”).newInstance() /> <cfdump var=#configurator# />