If I have a class that is already loaded how do I figure out from what
URL it was loaded from (actually local filename is all I care about)?
Arne Vajhøj - 23 Nov 2007 03:37 GMT
> If I have a class that is already loaded how do I figure out from what
> URL it was loaded from (actually local filename is all I care about)?
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;
}
}
Arne
Owen Jacobson - 23 Nov 2007 03:43 GMT
> If I have a class that is already loaded how do I figure out from what
> URL it was loaded from (actually local filename is all I care about)?
Assuming that the class was loaded from a file at all, you can get that
information from the class's ProtectionDomain. Have a look at
<http://java.sun.com/javase/6/docs/api/java/security/ProtectionDomain.html>
particularly the getCodeSource() method.
-O
Roedy Green - 23 Nov 2007 08:24 GMT
On Thu, 22 Nov 2007 15:56:46 -0800 (PST), "Aryeh M. Friedman"
<Aryeh.Friedman@gmail.com> wrote, quoted or indirectly quoted someone
who said :
>If I have a class that is already loaded how do I figure out from what
>URL it was loaded from (actually local filename is all I care about)?
see http://mindprod.com/jgloss/protectiondomain.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com