did u know u can find the location of the .class file from the instance??
In general there is no guarantee that u can always get the location(Basically URL) from where ur instance or object is loaded but luck u can’t completely discard.. 🙂
The following class which i am putting tries to find out the location of teh instance from where it is loaded. All you need to do is call the getClass method on your concerned instance and pass the output to this method.
package com.deepak.classloader;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
public class FindClassLoaction {
public static URL getClassLocation (final Class clazz)
{
URL clazzLocation = null;
final String clazzFileName = clazz.getName().replace (‘.’, ‘/’).concat (“.class”);
final ProtectionDomain pd = clazz.getProtectionDomain ();
if (pd != null)
{
final CodeSource codeSourceOfClazz = pd.getCodeSource();
if (codeSourceOfClazz != null) clazzLocation = codeSourceOfClazz.getLocation ();
if (clazzLocation != null)
{
if (“file”.equals (clazzLocation.getProtocol ()))
{
try
{
if (clazzLocation.toExternalForm ().endsWith (“.jar”) ||
clazzLocation.toExternalForm ().endsWith (“.zip”))
clazzLocation = new URL (“jar:”.concat (clazzLocation.toExternalForm ())
.concat(“!/”).concat (clazzFileName));
else if (new File (clazzLocation.getFile ()).isDirectory ())
clazzLocation = new URL (clazzLocation, clazzFileName);
}
catch (MalformedURLException ignore) {}
}
}
}
if (clazzLocation == null)
{
final ClassLoader clsLoader = clazz.getClassLoader ();
clazzLocation = clsLoader != null ?
clsLoader.getResource (clazzFileName) :
ClassLoader.getSystemResource (clazzFileName);
}
return clazzLocation;
}
}
The luck factor here is that u can’t guarantee the fact that every class is loaded with some ProtectionDomain i.e. It depends on the classloader whether it is creating the ProtectionDomain before loading the class….:(
also the CodeSource can be null depending on the classloader implementation. U can almost be sure that protection domain and code source will be populated for the classloders which are instance of URLClassLoader. So better you can put a check on the claasloader whether its an instance of URLClassLoader to be on safe side.
Sun’s implementation seems to allow finding the class file as resource ..
enjoy….
6 Comments »
Leave a Reply
-
Recent
- interesting fact about identityHashCode() method in java.lang.System Class
- Interesting inconsistency with java String pool
- did u know u can find the location of the .class file from the instance??
- Cases for Shortcomings of Single Request-Response model
- Information asymmetry
- Another bad mark on My Image in Boss’s Mind……:(
- Sharing the same connection or transaction space between derby sql and java.
- Did u know that u can shutdown someone’s machine(windows) using java????
- Utilizing JAVA’s power in Derby Database Triggers
- Dids u know that u can search across multiple columns in a database table??
- Did you know that you can compare RAW columns in a DB?
-
Links
-
Archives
- July 2008 (2)
- April 2008 (2)
- September 2007 (5)
- August 2007 (1)
- May 2007 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS
[…] blog about this after I spoke with Deepak about this. I didn’t know he had a blog and he had blogged about the same topic too! […]
Pingback by 0xCAFEFEED » Where was this class loaded from?! | May 19, 2008 |
good material thanks
this is be cool 8)
I don’t get it. Wouldn’t the following code get you the same results, but at the same time more reliable?
public static URL getLocation(Class rClass)
{
return rClass.getResource(“/” + rClass.getName().replace(‘.’, ‘/’) + “.class”);
}
[…] blog about this after I spoke with Deepak about this. I didn’t know he had a blog and he had blogged about the same topic […]
Pingback by Where was this class loaded from?! - Akhil’s Blog | February 1, 2009 |
thank you for this code, it’s very useful