Question: I have read a lot about the Java class loading process lately. Often I came across texts that claimed that it is not possible to add classes to the classpath during runtime and load them without class loader hackery (URLClassLoaders etc.)

As far as I know classes are loaded dynamically. That means their bytecode representation is only loaded and transformed to a java.lang.Class object when needed.

So shouldn't it be possible to add a JAR or \*.class file to the classpath after the JVM started and load those classes, provided they haven't been loaded yet? (To be clear: In this case the classpath is simply folder on the filesystem. "Adding a JAR or \*.class file" simply means dropping them in this folder.)

And if not, does that mean that the classpath is searched on JVM startup and all fully qualified names of the found classes are cached in an internal "list"?

It would be nice of you if you could point me to some sources in your answers. Preferably the offical SUN documentation: [Sun JVM Spec](http://java.sun.com/docs/books/jvms/second_edition/html/VMSpecTOC.doc.html). I have read the spec but could not find anything about the classpath and if it's finalized on JVM startup.

P.s.

This is a theoretical question. I just want to know if it is possible. There is nothing practical I want to achieve. There is just my thirst for knowledge :)

Answer: 
There are two concepts here that are being intermixed: The classpath and the class files in the classpath.

If you point the classpath to a directory, you will generally have no issue adding a file to the directory and having it picked up as part of the classpath. Due to the potential size of all classes in the classpath it isn't really feasible for a modern JVM to load them all at startup. However this is of limited value as it will not include Jar files.

However, changing the classpath itself (which directories, jars, etc. are searched) on a running JVM will depend very much on the implementation. As far as I know, on standard Sun JVMs there is no documented (as in guaranteed to work) method of accomplishing this.

In general, if this is something you need to do (have a dynamic classpath that changes at runtime) then you want to be implementing a ClassLoader, if for no other reason than to be able to throw it away and make a new one that doesn't reference those classes anymore if they need to be unloaded.

However, for a small amount of dynamic loading there are better ways. In Java 1.6 you can specify all the jar files in a directory (\*.jar) so you can tell users to put additional libraries in a specified location (although they have to be there at startup).

You also have the option of including a jar file or other location in the classpath even though you don't need it, as a placeholder for someone to put an optional jar or resource file there (such as a log configuration file).

But if you need serious dynamic class loading and especially unloading while the application is running, a Classloader implementation is required.
> 
> So shouldn't it be possible to add a
>  JAR or \*.class file to the classpath
>  after the JVM started ...
> 
> 
> 

You add jars and directories to the classpath, not classes. The classes are in either the directory, or the jar. 

> 
> And if not, does that mean that the
>  classpath is searched on JVM startup
>  and all fully qualified names of the
>  found classes are cached in an
>  internal "list"?
> 
> 
> 

That could be easily tested: Set the classpath, start your program, move a new class into the CP, call 'Class.forName ("NewClass") from your program. Does it find the new class?