查找从何处加载 java 类 [英] Find where java class is loaded from

查看:32
本文介绍了查找从何处加载 java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何以编程方式找出 java 类加载器实际从何处加载类?

Does anyone know how to programmaticly find out where the java classloader actually loads the class from?

我经常在类路径很长并且手动搜索不是真正的选择的大型项目中工作.我最近遇到了一个问题,其中类加载器加载了错误版本的类,因为它位于两个不同位置的类路径上.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆