Reputation: 2945
How would you go about establishing where a class ( or maybe resource ) has been loaded from?
I am trying to work out exactly where a class has been loaded from. Does anyone know if you can find out the following:
Upvotes: 4
Views: 426
Reputation: 408
if you have an instance of the class insta.getClass().getClassLoader().getName() -->this is the classloader that loaded the class
insta.getClass().getProtectionDomain().getCodeSource().getLocation()- where the class was loader from
Upvotes: 0
Reputation: 91881
I would go about that with the -verbose:class
command line option, saving the output to a text file and then use grep (or the DOS find if you are windows) to see details on how that class is loaded.
Upvotes: 0
Reputation: 147164
For a class one solution is MyClass.class.getProtectionDomain().getCodeSource().getLocation()
. (Actually, may have a null
ProtectionDomain
or that may not hava a CodeSource
. My also throw a SecurityException
.
Upvotes: 3
Reputation: 32831
You can get an URL by calling MyClass.class.getResource("MyClass.class") for instance.
Upvotes: 2
Reputation: 26418
i think you will get the answer here :
http://www.jugpadova.it/articles/2005/11/13/from-which-jar-a-class-was-loaded
http://www.techtalkz.com/java/102114-how-find-jar-file-ava-class-all-successorshad-been-loaded.html
Upvotes: 1
Reputation: 10961
The class Class
has an instance method getClassLoader()
that returns a reference to the class loader that loaded the class it represents. Note that this can return null
. See here.
So, if you wanted to know which classloader loaded String
(just as an example) you could do:
ClassLoader loader = String.class.getClassLoader();
or:
ClassLoader loader = "I'm a String".getClass().getClassLoader();
Upvotes: 5