Log in

No account? Create an account
Where is ColdFusion finding my java class files? - Nick [entries|archive|friends|userinfo]

[ website | gagravarr.org ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Where is ColdFusion finding my java class files? [Jun. 1st, 2005|05:29 pm]
(This applies to any Java system too, but we've found it most useful with ColdFusion. We managed to use it twice in 5 minutes after cracking it!)

I'm sure you know the problem. A Java custom tag is misbehaving, or a CreateObject("java","...") is returning an old version of your class. You have half a dozen jar files in the classpath, a few in /lib/, and a few directories of class files. Where on earth is the classloader finding it?

The answer is to instanciate the class, grab it's classloader, then use that to load the .class file as a resource. The resource you get back offers a getPath() method, and that will show where the class came from. Simple!

The code to do this is easy too. Let's say we're trying to track down the class for a custom tag cfx_Misbehave. We take a look in the CFX tag definition, and spot it is provided by "com.torchbox.cfx.Misbehave". Now, code up a quick cfm file with the following:

<cfset obj = CreateObject("java","com.torchbox.cfx.Misbehave")>
<cfset classloader = obj.getClass().getClassLoader()>
<cfset res = classloader.getResource("com/torchbox/cfx/Misbehave.class")>
<cfset pathToClass = res.getPath()>
<cfdump var="#pathToClass#">

Run that, and you'll get handy output like "/usr/local/Java/com/torchbox/cfx/Misbehave.class" or "file:/usr/local/Java/classpath/Misbehave.jar!/com/torchbox/cfx/Misbehave.class". Bingo, location found :)

In pure Java, it's even easier:

com.torchbox.cfx.Misbehave misbehave = new com.torchbox.cfx.Misbehave();
ClassLoader cl = misbehave.getClass().getClassLoader();
java.net.URL resource = cl.getResource("com/torchbox/cfx/Misbehave.class");