How to find the JAR file from which a ava class and all successorshad been loaded.

P

Peter

Hi,

I am looking for some piece of java code which allows me to get the name
of the JAR file from which a java class (and all subsequent classes
which are used in this java class ) had been loaded by the classloader.

Any hint or idea or - even better - code is appreciated.

Thanks,


Peter
 
A

Andrew Thompson

Peter wrote:
...
I am looking for some piece of java code which allows me to get the name
of the JAR file from which a java class (and all subsequent classes
which are used in this java class ) had been loaded ..

Class files might not be in a Jar file. They might
be loose class files, or created on the fly.

Why do you think you need to know this information?
(Try and answer that in terms of "I want to offer the
end user 'X'", where X is something an end-user might
care about.)

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200709/1
 
S

Stefan Ram

?

=?ISO-8859-15?Q?Arne_Vajh=F8j?=

Peter said:
I am looking for some piece of java code which allows me to get the name
of the JAR file from which a java class (and all subsequent classes
which are used in this java class ) had been loaded by the classloader.

Any hint or idea or - even better - code is appreciated.

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top