Tuesday, September 15, 2009

Find the location of your classfile

There are many reasons for tring to find what location your java class is loaded from. E.g a big jee server with many apps and conflicting versions of libraries (read: a mastodont of a Websphere server)...

Here is the code that has saved many a day for me:
private String getPath(Class cls) {
String cn = cls.getName();
String rn = cn.replace('.', '/') + ".class";
String path = getClass().getClassLoader().getResource(rn).getPath();
int ix = path.indexOf("!");
if(ix >= 0) {
return path.substring(0, ix);
} else {
return path;
}
}

No comments:

Post a Comment