How do I tell the URLClassLoader where to find classes to load?

John Zukowski

Introduced with the Java 2 platform, the URLClassLoader just requires a URL array of where to find classes. In the URL list can be directories, ZIP files, or JAR files.

try {
  URL[] urlList = {
    new URL("http://java.sun.com/share/classes/"),
    new URL("http://www.jars.com/jediSearch.zip"),
    new URL("http://java.about.com/library/weekly/"),
    new File("myJar.jar").toURL()};
  ClassLoader loader = new URLClassLoader(urlList);
  Class c = loader.loadClass("TheClass");
  TheClass tc = (TheClass) c.newInstance();
} catch (MalformedURLException e) {
  // load classes another way or display error message