Wednesday, December 24, 2008

Merry Christmas

I just wanted to say Merry Christmas and Happy Holidays to everyone!

Yes Virginia, there is a Santa Claus

...Read More

Wednesday, December 17, 2008

Determining which jar file a class was loaded from

A reader named Murray happened to ask if there is a way to determine which jar file a class was loaded from. It is an invaluable bit of information when working with new jars. Nothing worse than spending an hour trying to debug code, only to find out the real problem was you were working with the wrong version of a class all along.

In case you are not already aware of it, an old entry on Brandon Purcell's blog provides an excellent tip on how to retrieve this information: Identifying Which Jar file a class was loaded from in ColdFusion MX . It uses getClassLoader() to retrieve information about the parent jar. Now since it uses the default class loader, it works perfectly well for jars in the CF classpath. But as I belatedly remembered, the javaLoader uses a separate class loader (duh!). So if you are checking a class that was loaded by the javaLoader you will need to make a few tweaks.

Here is a quick example using one of the POI classes. The code is simple and essentially does the same thing as Brandon's function. The only difference being it should work with objects loaded by a different class loader as well.

// get a reference to the javaLoader
javaLoader = server[ application.myJavaLoaderKey ];

// create test workbook object
wb = javaLoader.create("org.apache.poi.hssf.usermodel.HSSFWorkbook");

// get the class name (without the package) ie "HSSFWorkbook.class"
className = listLast( wb.getClass().getName(), ".") &".class";
resourceURL = wb.getClass().getResource( className );

if ( isDefined("resourceURL") ) {
else {
WriteOuput("Could not resolve resource");

...Read More

  © Blogger templates The Professional Template by 2008

Header image adapted from atomicjeep