Find out location of a class file from where its loaded at runtime in Java
Recently one of my coleague was facing issue Invalid class loader hierarchy. You have more than one version of 'org.apache.commons.logging.Log' visible, which is not allowed.
In our class path we found only one version of jar. I removed our jar from classpath it was still giving the same error. I was amazed and done System.out.println(System.getProperty("java.class.path"))
which also didn’t provided anything meaningful, I was clueless where the jar was located.
Finally we come to the conclusion that some how we need to get the location of the class in question from where it was being loaded. But how? After some googling we found that following is the code to get the location of the class file
org.apache.commons.logging.Log.class.getProtectionDomain().getCodeSource().getLocation().toURI()
We removed the jar in question from server library and it worked.
Leave a Reply