KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > ClassLoader

java.lang
Class ClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
Direct Known Subclasses:
SecureClassLoader
See Also:
Top Examples, Source Code, String, defineClass, findClass, loadClass, Class.newInstance, defineClass, reference, resolveClass(Class)

protected ClassLoader()
See Also:
SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[495]Can not load application level classes from the system classloader
By Anonymous on 2003/11/06 18:02:16  Rate
 
 //The following code will fail, because it is trying to load application level classes from the system classloader which is not going to happen unless the jar is in the jre/lib/ext directory  ( which would be a "bad thing" ) . 
 ClassLoader cl = ClassLoader.getSystemClassLoader (  )  ;  
 testClass = cl.loadClass ( "test.TestClass1" )  ; 
    
 //You could do something like: 
 this.getClass (  ) .getClassLoader (  ) .loadClass ( "whatever" ) ; 
  
  
 


[502]Load resource from context
By Anonymous on 2005/03/21 09:16:00  Rate
BufferedReader br = null; 
 try  {  
 br = new BufferedReader 
      ( new InputStreamReader ( Thread.currentThread (  )  
          .getContextClassLoader (  )  
          .getResourceAsStream  ( "aFile.txt" )  )  ) ; 
  }  catch  ( Exception fnf )   {   } 


[1837]How to find the file from which the class has been loaded
By Sergey Zykov on 2006/10/30 02:41:50  Rate
private String classNameToPath ( final String className )   {  
     return new StringBuffer (  ) .append ( className.replace ( '.', '/' )  ) .append ( ".class" ) .toString (  ) ; 
    }  
  
  
  
      final String className = param.getClass (  ) .getName (  ) ; 
         cruft.append ( "Class : \"" ) .append ( className )  
                 .append ( "\", Hashcode : \"" ) .append ( param.hashCode (  )  )  
                 .append ( "\", String representation : \"" ) .append ( param.toString (  )  )  
                 .append ( "\". " ) ; 
         final ClassLoader cl = param.getClass (  ) .getClassLoader (  )  != null ? 
                 param.getClass (  ) .getClassLoader (  )  
                 : ClassLoader.getSystemClassLoader (  ) ; 
  
  
         cruft.append ( " ClassLoader: \"" )  
                 .append ( cl.toString (  )  )  
                 .append ( "\". " ) ; 
         final String classNamePath = classNameToPath ( className ) ; 
         final URL res = cl.getResource ( classNamePath ) ; 
         cruft.append ( " ResourcePath: \"" )  
               .append ( res != null ? res.toExternalForm (  )  : " < null > " )  
               .append ( "\"" ) ;


protected ClassLoader(ClassLoader parent)
See Also:
SecurityException, checkCreateClassLoader
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1769]Standard classloaders uses single namespace to load classes
By Anonymous on 2006/06/02 11:23:04  Rate
One of the biggest problems when dealing with shared libraries in Java, using standard classloaders, is the single namespace into which all classes are loaded. This makes it very difficult to use different versions of the same library at any given time.

[1770]Use different versions of the same library at any given time
By Anonymous on 2006/06/02 11:26:50  Rate
One of the biggest problems when dealing with shared libraries in Java, using standard classloaders, is the single namespace into which all classes are loaded. This makes it very difficult to use different versions of the same library at any given time. What is needed is the ability for a component to define its own namespace into which the component and all of its ancillary libraries reside. 
 Since the runtime identity of a class in Java is defined by the class???s fully-qualified name and the ID of its classloader, a namespace already exists for each classloader. Therefore, the classloader can be used to build a component container that defines a namespace for a component and its dependencies. 
 


public void clearAssertionStatus()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
protected final Class<?> defineClass(byte[] b,
                                                int off,
                                                int len)
                              throws ClassFormatError
See Also:
resolveClass(Class), loadClass(String, boolean), IndexOutOfBoundsException, defineClass(String, byte[], int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected final Class<?> defineClass(String name,
                                     byte[] b,
                                     int off,
                                     int len)
                              throws ClassFormatError
See Also:
SecureClassLoader, CodeSource, resolveClass(Class), loadClass(String, boolean), SecurityException, IndexOutOfBoundsException, defineClass, Policy.getPolicy().getPermissions(new CodeSource(null, null)), ProtectionDomain
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected final Class<?> defineClass(String name,
                                     byte[] b,
                                     int off,
                                     int len,
                                     ProtectionDomain protectionDomain)
                              throws ClassFormatError
See Also:
SecurityException, IndexOutOfBoundsException, NoClassDefFoundError, CodeSource, defineClass(String, byte[], int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected final Class<?> defineClass(String name,
                                     ByteBuffer b,
                                     ProtectionDomain protectionDomain)
                              throws ClassFormatError
See Also:
defineClass(String, byte[], int, int, ProtectionDomain), SecurityException, NoClassDefFoundError, get, remaining, defineClass(String, byte[], int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Package definePackage(String name,
                                String specTitle,
                                String specVersion,
                                String specVendor,
                                String implTitle,
                                String implVersion,
                                String implVendor,
                                URL sealBase)
                         throws IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


protected String findLibrary(String libname)
See Also:
System.mapLibraryName(String), System.loadLibrary(String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected final Class<?> findLoadedClass(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


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


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


protected Package getPackage(String name)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected Package[] getPackages()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final ClassLoader getParent()
See Also:
SecurityException, RuntimePermission("getClassLoader"), checkPermission
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


[857]Load resource from CLASSPATH
By Anonymous on 2004/08/12 15:33:57  Rate
//The directory where MyConfig.xml resides should be in the CLASSPATH 
  
  
 ClassLoader classLoader = ClassLoader.getSystemClassLoader (  ) ; 
 URL url = classLoader.getResource ( "MyConfig.xml" ) ;     
 System.out.println ( "the config file is " + url.getFile (  )  ) ;


[1759]
By ghram_hossien { at } yahoo { dot } com on 2006/05/24 14:03:12  Rate
java.util.PropertyResourceBundle

public InputStream getResourceAsStream(String name)
See Also:
getResource(String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[964]_
By Anonymous on 2004/10/01 14:37:39  Rate
Both Class and Classloader have a method getResourceAsStream ( String )  for opening a file from the class path.

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


public static ClassLoader getSystemClassLoader()
See Also:
Throwable.getCause(), Error, IllegalStateException, SecurityException, RuntimePermission("getClassLoader"), checkPermission
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


public static InputStream getSystemResourceAsStream(String name)
See Also:
getSystemClassLoader()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[779]Print ByteCode
By Grzegorz on 2004/05/14 07:27:47  Rate
import java.io.IOException; 
 import java.io.InputStream; 
 public class PrintMyByteCode  {  
   public static void main ( String [  ]  args )  throws IOException  {    
     InputStream in = ClassLoader.getSystemResourceAsStream (  
                      "PrintMyByteCode.class" 
                       ) ; 
     int chr = 0; 
     while  (   ( chr = in.read (  )  )   > = 0 )  System.out.write ( chr ) ; 
     in.close (  ) ; 
    }  
  }  
 


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


public Class<?> loadClass(String name)
                   throws ClassNotFoundException
See Also:
loadClass(name, false)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[965]Compatibility issue with loadClass
By Anonymous on 2004/10/01 14:39:41  Rate
This does not work on some systems, eg FreeBSD java 1.1.8, the class loader may be null because the default class loader is signified by a null.  
  
  
 A better method is:  
 Class cls = Class.forName ( classname ) ;


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


[1768]Java runtime classes loading order
By Anonymous on 2006/06/02 11:18:30  Rate
A Java runtime typically finds and loads classes in the following order:  
 ??In the list of bootstrap classes ??? Classes which embody the Java platform, such as the classes in rt.jar.  
 ??In the list of extension classes ??? Classes utilizing the Extension Mechanism Framework  ( http://java.sun.com/j2se/1.4.2/docs/guide/extensions/spec.html )  to extend the Java platform, stored in archive files  ( .jar, .zip, etc. )  located in the  < java-home > /lib/ext directory of the runtime environment.  
 ??User classes ??? Classes that do not utilize the extension mechanism architecture identified using the -classpath command-line option or the CLASSPATH environment variable. 
 


protected final void resolveClass(Class<?> c)
See Also:
defineClass(String, byte[], int, int), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setClassAssertionStatus(String className,
                                    boolean enabled)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setDefaultAssertionStatus(boolean enabled)
See Also:
setClassAssertionStatus(String, boolean), setPackageAssertionStatus(String, boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void setPackageAssertionStatus(String packageName,
                                      boolean enabled)
See Also:
setClassAssertionStatus(String, boolean)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected final void setSigners(Class<?> c,
                                Object[] signers)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags