Hands up if you’ve seen this:
You deploy your application. Crash. Time to switch on more logging by editing log4j.properties. You change it and nothing happens. Was there another log4j.properties somewhere? Which one is being used?
You made your change and the build is still picking up old code. Where’s that being loaded?
Wouldn’t it be nice to get some more info from the classloader? I hereby introduce FindClass which, despite the name, can find any resource really, whether nested in a sub folder or buried inside a JAR file.
Here’s a quick example:
This will print out:
Long class has been loaded from : D:\Java\jdk1.6.0_02\jre\lib\rt.jar FindClass class has been loaded from : D:\Workspace\\findclass\target\classes\com\jolbox\utils\FindClass.class
Just for kicks, I’ve added helper methods on the same theme so that you can trace an existing object, a given class or a resource:
This is nice but sometimes you need to find stuff without having to embed this inside your application. Using basically the same methods, the class itself can be made to search directly from the command line:
java -cp . com.jolbox.utils.FindClass startdir match extension
FindClass d:\\workspace FooBar .class
matches *FooClass*.class (case is not important)
What if you’re application is already running? Not to fear! So long as your application is Spring enabled (and you’ve scanned the class in), the more important methods are already JMX enabled.
Going back to our original question: Want to know all the locations of log4j.properties? Just call:
at the beginning of your application to get a list of locations containing that file.
There are some other helper methods available but I’ll leave those for you to discover. Do remember however that these methods are slow and are therefore only to be used in a debugging context.
FAQ: Can you dump all the locations of all the classes in use?
Answer: No… and yes. The method is available but remember that the classloader will only load the classes you actually use so it’s really a dump of all the locations of the classes that have been used at least once.
Disclaimer: Not all the code in the class was written by me; some routines are a collection of snippets off the web.
Download code from here
JolBox hosts BoneCP – The fast Java JDBC Connection pool.