Click here to Skip to main content
Licence CPOL
First Posted 20 Jan 2011
Views 6,372
Downloads 50
Bookmarked 3 times

Adding class paths to SystemClassLoader

By | 23 Jan 2011 | Article
How to add class paths to SystemClassLoader.

Introduction

This article is a non-recommended hack for dynamically adding a class path to SystemClassLoader. You should re-think your design before using this hack; usually, you should be able to use URLClassLoader to load whatever you want.

About SystemClassLoader

SystemClassLoaders are usually URLClassLoaders, and in URLClassLoader, there is a method called addURL(), so we can make use of this to add paths/jars to SystemClassLoader dynamically at runtime. However, since "addURL()" is a protected method, we have to use Reflection to call it.

The sample code is as follows:

public static void addURLs(URL[] urls) {
  ClassLoader cl = ClassLoader.getSystemClassLoader();
  if ( cl instanceof URLClassLoader ) {
    URLClassLoader ul = (URLClassLoader)cl;
    // addURL is a protected method, but we can use reflection to call it
    Class<?>[] paraTypes = new Class[1];
    paraTypes[0] = URL.class;
    Method method = URLClassLoader.class.getDeclaredMethod("addURL", paraTypes);
    // change access to true, otherwise, it will throw exception
    method.setAccessible(true);
    Object[] args = new Object[1];
    for(int i=0; i<urls.length; i++) {
      args[0] = urls[i];
      method.invoke(ul, args);
    }
  } else {
    // SystemClassLoader is not URLClassLoader....
  }
}

Test

I have written a test case, and it will automatically load all "lib/*.jar" and instantiate an object as specified in args[0]. The test output is as follows. Note: I only included "bin" as my class path, and I have commons-collections.jar under my "lib" directory, but the program is still able to dynamically load the required class.

History

  • 2010.01.20: First draft.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sam NG



Hong Kong Hong Kong

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralInteresting, but well known and not recommended hack PinmemberAlexandre Nikolov22:19 20 Jan '11  
GeneralRe: Interesting, but well known and not recommended hack PinmemberSam NG16:50 23 Jan '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 23 Jan 2011
Article Copyright 2011 by Sam NG
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid