Find where java class is loaded from

前端 未结 11 1265
独厮守ぢ
独厮守ぢ 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()

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-22 06:35

    Edit just 1st line: Main.class

    Class<?> c = Main.class;
    String path = c.getResource(c.getSimpleName() + ".class").getPath().replace(c.getSimpleName() + ".class", "");
    
    System.out.println(path);
    

    Output:

    /C:/Users/Test/bin/
    

    Maybe bad style but works fine!

    0 讨论(0)
  • 2020-11-22 06:36

    Take a look at this similar question. Tool to discover same class..

    I think the most relevant obstacle is if you have a custom classloader ( loading from a db or ldap )

    0 讨论(0)
  • 2020-11-22 06:38

    Jon's version fails when the object's ClassLoader is registered as null which seems to imply that it was loaded by the Boot ClassLoader.

    This method deals with that issue:

    public static String whereFrom(Object o) {
      if ( o == null ) {
        return null;
      }
      Class<?> c = o.getClass();
      ClassLoader loader = c.getClassLoader();
      if ( loader == null ) {
        // Try the bootstrap classloader - obtained from the ultimate parent of the System Class Loader.
        loader = ClassLoader.getSystemClassLoader();
        while ( loader != null && loader.getParent() != null ) {
          loader = loader.getParent();
        }
      }
      if (loader != null) {
        String name = c.getCanonicalName();
        URL resource = loader.getResource(name.replace(".", "/") + ".class");
        if ( resource != null ) {
          return resource.toString();
        }
      }
      return "Unknown";
    }
    
    0 讨论(0)
  • 2020-11-22 06:38

    Simple way:

    System.out.println(java.lang.String.class.getResource(String.class.getSimpleName()+".class"));

    Out Example:

    jar:file:/D:/Java/jdk1.8/jre/lib/rt.jar!/java/lang/String.class

    Or

    String obj = "simple test"; System.out.println(obj.getClass().getResource(obj.getClass().getSimpleName()+".class"));

    Out Example:

    jar:file:/D:/Java/jdk1.8/jre/lib/rt.jar!/java/lang/String.class

    0 讨论(0)
提交回复
热议问题