getting the url of a class

A

Aryeh M. Friedman

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)?
 
A

Arne Vajhøj

Aryeh 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)?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top