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
Andrew Thompson - 23 Sep 2007 16:17 GMT
...
>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.)

Signature
Andrew Thompson
http://www.athompson.info/andrew/
Stefan Ram - 23 Sep 2007 16:23 GMT
>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.
For the standard class loader, the system resource
corresponding to a class path (such as
»java/lang/Object.class«) can be obtained via
http://download.java.net/jdk7/docs/api/java/lang/ClassLoader.html#getSystemResou
rce(java.lang.String)
Lew - 23 Sep 2007 17:31 GMT
>> 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
[quoted text clipped - 5 lines]
>
> http://download.java.net/jdk7/docs/api/java/lang/ClassLoader.html#getSystemResou
rce(java.lang.String)
That gets the system resource given the name. The OP wants to get the name of
the JAR file from which a resource came, not the name of the resource,
assuming the resource came from a JAR, given the resource.

Signature
Lew
Real Gagnon - 23 Sep 2007 16:38 GMT
> 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.
"Determine if running from JAR"
http://www.rgagnon.com/javadetails/java-0391.html

Signature
Real Gagnon from Quebec, Canada
* Java, Javascript, VBScript and PowerBuilder code snippets
* http://www.rgagnon.com/howto.html
* http://www.rgagnon.com/bigindex.html
Arne Vajhøj - 23 Sep 2007 18:32 GMT
> 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