Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

NestedURLClassLoader



The NestedURLClassLoader class allows to load classes which are in jar files nested inside archives. The loading is not formed by crating temporary copies of the jar files outside the archive but by directly loading the classes byte content.

Note that by default Java ClassLoaders (including URLClassLoader) can not load classes which are in jar files nested inside archives.

Foreword

This class is derived from the github.com/squark-io/nested-jar-classloader project. A lot of things have been changed from the initial class, which are:
  • There are no more external dependencies on the class[1]
    The initial project depended on a lot of external libraries, including slf4j, Apache commons collections, jetbrains, google.common.io, and also the specific non public API sun.net.www.ParseUtil class
  • The notion of module has been removed to simplify the structure[2]
    The initial project allow to define a graph of Class loaders

Usage

This class can be used exactly like the URLClassLoader class, but contrary to the URLClassLoader, you can load classes which are in jar files nested inside archives.

For example suppose that we have the following zip file structure:
      zipfile.zip
         - TestEmbed.jar
and the the TestEmbed.jar jar file containing the org.testutils.embed.EmbeddedClass class with a no argument constructor.

We can do:
   File zipFile = new File("zipfile.zip");
   URL zipURL = zipFile.toURI().toURL();
   URL jarURL = FileUtilities.getJarEntryURL(zipURL, "TestEmbed.jar");

   URL[] urls = new URL[1];
   urls[0] = jarURL;
   NestedURLClassLoader loader = new NestedURLClassLoader(urls);
   Class clazz = Class.forName("org.testutils.embed.EmbeddedClass", true, loader);
   Object o = clazz.newInstance();

Filtering packages from the special loading mechanism

It is possible to filter packages from the special loading mechanism used by this class by using the NestedURLClassLoader.filterPackages(boolean, Set) or the NestedURLClassLoader.filterPackages(boolean, String...) method.

There are two modes for this feature:
  • If excludingFilter is true: packages which are in the Set will be loaded using the default behavior of the URLClassLoader, all other packages will be loaded using the special behavior of this Class
  • If excludingFilter is false: packages which are not in the set will be loaded using the default behavior of the URLClassLoader, all packages in the Set will be loaded using the special behavior of this Class

Notes

  1. ^ The initial project depended on a lot of external libraries, including slf4j, Apache commons collections, jetbrains, google.common.io, and also the specific non public API sun.net.www.ParseUtil class
  2. ^ The initial project allow to define a graph of Class loaders

See also


Categories: lang | packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences