Find where java class is loaded from

前端 未结 11 1288
独厮守ぢ
独厮守ぢ 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:28

    This is what we use:

    public static String getClassResource(Class klass) {
      return klass.getClassLoader().getResource(
         klass.getName().replace('.', '/') + ".class").toString();
    }
    

    This will work depending on the ClassLoader implementation: getClass().getProtectionDomain().getCodeSource().getLocation()

提交回复
热议问题