Search code examples
javaclasspathosgiclassloaderencapsulation

Is there a Tomcat-like classloader that can be used standalone?


I'm working with a Java sort-of-application-server (Smartfox) which can run multiple applications ("extensions") but has a very inconvenient classpath setup to go along with it, along with issues when trying to use SLF4J.

To work around that I'd like to wrap my applications in their own classloaders. Such a containing classloader should be much like Tomcat's, in that it

  • Can load classes from a directory containing JARs.
  • Prefers classes from its own classpath over those from the parent

Is there a library somewhere that has such a classloader I can just "drag and drop" in my project? If not, would it be hard to create it myself? Any known pitfalls?


Solution

  • OSGi (and other module systems) are designed to handle exactly this kind of problem.

    It might look like overkill at first, but I think you'll quickly re-implement significant parts of the things that OSGi alread does for you.

    Equinox is the OSGi implementation used by Eclipse, for example.