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
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact