查找从哪里加载java类 [英] Find where java class is loaded from
问题描述
有谁知道如何以编程方式找出java类加载器实际加载类的位置?
Does anyone know how to programmaticly find out where the java classloader actually loads the class from?
我经常在大型项目上工作,其中类路径变得非常长并且手动搜索实际上不是一个选项。我最近有一个问题,其中类加载器加载的版本不正确class,因为它位于两个不同位置的类路径上。
I often work on large projects where the classpath gets very long and manual searching is not really an option. I recently had a problem where the classloader was loading an incorrect version of a class because it was on the classpath in two different places.
那么如何让类加载器告诉我实际的类文件来自磁盘的哪个位置?
So how can I get the classloader to tell me where on disk the actual class file is coming from?
编辑: 如果由于版本不匹配(或其他原因)类加载器实际上无法加载类怎么办? ,无论如何我们可以在它读取之前找出它试图读取的文件吗?
What about if the classloader actually fails to load the class due to a version mismatch (or something else), is there anyway we could find out what file its trying to read before it reads it?
推荐答案
这是一个例子:
package foo;
public class Test
{
public static void main(String[] args)
{
ClassLoader loader = Test.class.getClassLoader();
System.out.println(loader.getResource("foo/Test.class"));
}
}
打印出来:
file:/C:/Users/Jon/Test/foo/Test.class
这篇关于查找从哪里加载java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!