[英]Where did this class come from?
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: 有谁知道你是否能找到以下内容:
The class Class
has an instance method getClassLoader()
that returns a reference to the class loader that loaded the class it represents. 类Class
有一个实例方法getClassLoader()
,它返回对加载它所代表的类的类加载器的引用。 Note that this can return null
. 请注意,这可以返回null
。 See here . 看到这里 。
So, if you wanted to know which classloader loaded String
(just as an example) you could do: 所以,如果你想知道哪个类加载器加载了String
(就像一个例子),你可以这样做:
ClassLoader loader = String.class.getClassLoader();
or: 要么:
ClassLoader loader = "I'm a String".getClass().getClassLoader();
例如,您可以通过调用MyClass.class.getResource(“MyClass.class”)来获取URL。
For a class one solution is MyClass.class.getProtectionDomain().getCodeSource().getLocation()
. 对于第一类解决方案是MyClass.class.getProtectionDomain().getCodeSource().getLocation()
。 (Actually, may have a null
ProtectionDomain
or that may not hava a CodeSource
. My also throw a SecurityException
. (实际上,可能有一个null
ProtectionDomain
或者可能没有一个CodeSource
。我也抛出一个SecurityException
。
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.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 http://www.techtalkz.com/java/102114-how-find-jar-file-ava-class-all-successorshad-been-loaded.html
我会使用-verbose:class
命令行选项,将输出保存到文本文件,然后使用grep(或DOS查找,如果你是Windows)来查看有关如何加载该类的详细信息。
if you have an instance of the class insta.getClass().getClassLoader().getName() -->this is the classloader that loaded the class 如果你有一个类insta.getClass()的实例.getClassLoader()。getName() - >这是加载类的类加载器
insta.getClass().getProtectionDomain().getCodeSource().getLocation()- where the class was loader from insta.getClass()。getProtectionDomain()。getCodeSource()。getLocation() - 这个类是从哪里加载的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.