Java extends Application Classloader?

Problem: My company has many different cans / existing projects that they launch with their customers. For debugging purposes, we would like to generate a log to which each component is loaded, and more specifically: where . For example: stack.overflow.ClassA in the jar StackOverFlowJar.jar.

My thoughts: I experimented with passing each jar / existing project to a custom class loader. This custom class loader can potentially write the above information to a file and delegate the normal class load stream to its parent.

My CustomClassLoader extends ClassLoader, and I see that it loads classes, such as:

java.lang.Class
java.lang.Thread
java.lang.ThreadGroup

etc., all standard Java classes from the main library. These classes really reach my CustomClassLoader. However, as can be seen from the following image:

test source: http://javarevisited.blogspot.nl/2012/12/how-classloader-works-in-java.html

There are several classes of ClassLoaders. The Bootstrap ClassLoader (the one I'm producing with CustomClassLoader) is only responsible for the main libraries. If he cannot find the custom created class, he delegates it back to Extension ClassLoader. If the Extension ClassLoader cannot find it, it will pass it back to the Application ClassLoader, and I will see in the debug module, this is really the one that loads my custom classes. But this does not happen with my CustomClassLoader.

: Application ClassLoader ClassLoader? , ?

!

+4
2

Jar , , Jar . , URLClassLoader, , ClassLoader .

  public static void main (String... args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    URL[] classpath = new URL[0];

    LoggingClassLoader loggingClassLoader = new LoggingClassLoader(classpath, Main.class.getClassLoader());
    Class<?> aClass = loggingClassLoader.loadClass("com.company.project.RealMain");
    Method main = aClass.getMethod("main", args.getClass());
    main.invoke(null);

  }

Edit

: jar/URL, , jar:

  public static class LoggingClassLoader extends URLClassLoader {
    final List<ChildLogger> childLochildLoggersders = new ArrayList<>();

    public LoggingClassLoader(URL[] urls, ClassLoader parent) {
      super(urls, parent);
      for(URL url : urls) {
        childLochildLoggersders.add(new ChildLogger(url, this));
      }
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
      for(ChildLogger childLoader : childLochildLoggersders) {
        try {
          return childLoader.loadClass(name);
        } catch(ClassNotFoundException ignore) {
        }
      }
      throw new ClassNotFoundException(name);
    }

    private static class ChildLogger extends URLClassLoader {
      private final URL url;

      private ChildLogger(URL url, ClassLoader parent) {
        super(new URL[]{url}, parent);
        this.url = url;
      }

      @Override
      protected Class<?> findClass(String name) throws ClassNotFoundException {
        Class<?> aClass = super.findClass(name);
        System.out.println("Loaded from :" + url + " class: " + name);
        return aClass;
      }
    }

  }
+1

All Articles