Find where java class is loaded from

前端 未结 11 1397
独厮守ぢ
独厮守ぢ 2020-11-22 06:22

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 v

11条回答
  •  梦如初夏
    2020-11-22 06:31

    Typically, we don't what to use hardcoding. We can get className first, and then use ClassLoader to get the class URL.

            String className = MyClass.class.getName().replace(".", "/")+".class";
            URL classUrl  = MyClass.class.getClassLoader().getResource(className);
            String fullPath = classUrl==null ? null : classUrl.getPath();
    

提交回复
热议问题