KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > net > URLClassLoader

java.net
Class URLClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by java.security.SecureClassLoader
          extended by java.net.URLClassLoader
Direct Known Subclasses:
MLet
See Also:
Top Examples, Source Code

protected void addURL(URL url)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Package definePackage(String name,
                                Manifest man,
                                URL url)
                         throws IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Class<?> findClass(String name)
                      throws ClassNotFoundException
See Also:
ClassLoader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL findResource(String name)
See Also:
ClassLoader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration<URL> findResources(String name)
                               throws IOException
See Also:
ClassLoader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected PermissionCollection getPermissions(CodeSource codesource)
See Also:
SecureClassLoader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URL[] getURLs()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static URLClassLoader newInstance(URL[] urls)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static URLClassLoader newInstance(URL[] urls,
                                         ClassLoader parent)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URLClassLoader(URL[] urls)
See Also:
SecurityManager.checkCreateClassLoader(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[57]How JVM handles problem of name collisions
By Anonymous on 2004/10/24 07:50:14  Rate
/** The JavaTM Virtual machine1 handles problem of name collisions  
   * through its class loader architecture. Every class in an application 
   * is loaded by an associated ClassLoader object.  
   * 
   * How does this solve the name collision problem?  
   * The VM treats classes loaded by different class loaders as  
   * entirely different types, even if their packages and names are  
   * exactly the same. Here's a simple example:  
   */
 
  
  
  
 import java.net.*; 
  
  
 public class Loader  {  
   public static void main ( String  [  ]  args )   
     throws Exception 
    {  
     URL [  ]  urlsToLoadFrom = new URL [  ]  { new URL ( "file:subdir/" )  } ; 
     URLClassLoader loader1 = new URLClassLoader ( urlsToLoadFrom ) ; 
     URLClassLoader loader2 = new URLClassLoader ( urlsToLoadFrom ) ; 
     Class cls1 = Class.forName ( "Loadee", true, loader1 ) ; 
     Class cls2 = Class.forName ( "Loadee", true, loader2 ) ; 
     System.out.println ( " ( cls1 == cls2 )  is " +  (  ( cls1 == cls2 )  ? "true" : "false" )  ) ; 
    }  
  }  
                                                         
 //place Loadee in a subdir named 'subdir' 
 public class Loadee  {  
   static  {  
     System.out.println ( "Loadee class 
       loaded" ) ; 
    }  
  }  
  
  
  
 -------------------- 
 Compile the Loader class, then compile the Loadee class in a subdirectory  
 named "subdir." When you run the Loader class, you should see the output:  
  
  
  
     Loadee class loaded 
     Loadee class loaded 
      ( cls1 == cls2 )  is false         
  
  
  
 Both cls1 and cls2 are named Loadee. In fact, they both come from the same  
 .class file  ( although that does not need to be the case, in general ) .  
 Nevertheless, the VM treats cls1 and cls2 as two separate classes.  
  
  
 


public URLClassLoader(URL[] urls,
                      ClassLoader parent)
See Also:
SecurityManager.checkCreateClassLoader(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1771]Load a specific version of a component by name from a jar file
By Anonymous on 2006/06/02 11:34:28  Rate
import java.net.URL; 
 import java.net.URLClassLoader; 
  
  
 public class RestrictedURLClassLoader extends URLClassLoader 
  {  
   public RestrictedURLClassLoader ( URL [  ]  urls )  
    {  
      super ( urls, null ) ; 
    }  
  
  
   public Class loadClass ( String name )  
      throws ClassNotFoundException 
    {  
      Class cls = super.loadClass ( name ) ; 
  
  
      if  ( cls == null )  
       {  
         throw new ClassNotFoundException ( "Restricted ClassLoader" 
                                    + " is unable to find class: " 
                                    + name ) ; 
       }  
  
  
      return cls; 
    }  
  }  
  
  
  
  
 ============== 
  
  
 Uses the above class loader to finds a component by name from a jar file. 
  
  
 ClassLoader ctxClsLoader = Thread.currentThread (  ) .getContextClassLoader (  ) ; 
  
  
 URL url = ctxClsLoader.getResource ( "somefile.jar" ) ; 
 URL [  ]  urls = null; 
   
 urls = new URL [ 1 ] ; 
 urls [ 0 ]  = url; 
  
  
 RestrictedURLClassLoader urlClassLoader = new RestrictedURLClassLoader ( urls ) ; 
  
  
  try 
   {  
     Class cls = urlClassLoader.loadClass ( name ) ; 
     if  ( cls != null )  
      {  
        return cls; 
      }  
   }  
  catch  ( ClassNotFoundException e )  
   {  
     // ignore 
   }  
  }  
  
  
 


public URLClassLoader(URL[] urls,
                      ClassLoader parent,
                      URLStreamHandlerFactory factory)
See Also:
SecurityManager.checkCreateClassLoader(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags