Q:

MyClass.class.getResource("MyClass.class").getPath() returns different paths in JBoss 4.2.3 and JBoss 5.1.0

In one of my libraries (say abc.jar), I have a class MyClass.class. As I need to understand at run-time which directory the abc.jar file is present in, the following logic has been used.
String classPath = MyClass.class.getResource("MyClass.class").getPath();

In JBoss 4.2.3, this would result in classPath equal to something like this:
file:/D:/app-servers/jboss-4.2.3.GA/server/uat012/deploy/MyApp.war/WEB-
INF/lib/abc.jar!/com/foo/bar/MyClass.class

Whereas In JBoss 5.1.0, the classPath is returned as 
/D:/app-servers/jboss-5.1.0.GA/server/uat012/deploy/MyApp.war/WEB-
INF/lib/abc.jar/com/foo/bar/MyClass.class

The different is that it does not start with 'file' and the '!' character which splits the jar location and the class location within the jar is also missing.
So, is it because of the way the class loading differs in JBoss 5.1.0?
What is the best way to identify the location of the jar from a given class file independent of web/app server and normal JVM?

A:

The short answer is that it's not safe to do what you want, since it's not guaranteed that a the jar file you're trying to locate even exists.  What if the jar has been expanded?  What if the app server uses some other form of storage (i.e. in a database)?  The location of the jar is an implementation detail that should not be important to the application.
That said, if you MUST do this (indicative of bad design), you'll have to try it on each target platform and write platform-specific code, which you'll have to revisit each time you upgrade to the latest version.

