On Wed, 14 Jun 2000, Jacques Bergeron wrote:

> Hi everyone,
> 
> I am trying to run Ant directly from a Java program without having prior
> knowledge of Ant`s directory (cannot intialize classpath on program
> startup).
> 
> I would need to dynamically change the CLASSPATH once I know Ant`s directory
> (choosen by the user). Since changing the classpath is not possible (so was
> I told) I would need to make a custom ClassLoader!?
> 
> 1- Is it true that the classpath cannot be changed dynamically?
> 
> 2- If so do you know any exemple of a custom ClassLoader that I could use (I
> was told that Java`s URLClassLoader did not support the file: protocol). So
> I`m searching for a model.
> 
> Any help would be welcome :)

I've been vaguely looking at this recently. I don't know what the Java 2
API is like in this area, but classloaders aren't that hard - you just
need to extend ClassLoader and override findClass( String name ) to find
the class you're after. Something along the lines of:

public class FileClassLoader extends ClassLoader
{
        private Vector _classpath = new Vector();

        public FileClassLoader( String classpath )
        {
                StringTokenizer tokens = new StringTokenizer( classpath );
                while( tokens.hasMoreTokens() )
                {
                        _classpath.add( tokens.nextToken() );
                }
        }

        public Class findClass( String name )
        {
                StringBuffer nameBuffer = new StringBuffer();

                for( int i = 0; i < name.length(); i++ )
                {
                        if( name.charAt( i ) == '.' )
                                nameBuffer.append( "/" );
                        else
                                nameBuffer.append( name.charAt( i ) );
                }

                Enumeration classPaths = _classpath.elements();
                String testpath = null;
                File classFile = new File( );

                while( classPaths.hasMoreElements() && !classFile.exists() )
                {
                        testpath = (String)classPaths.nextElement() +
                                nameBuffer.toString();
                        classFile = new File( testpath );
                        if( classFile.exists() )
                        {
                                FileInputStream classStream = new
                                        FileInputStream( classFile );
                                byte[] classBytes =
                                        new byte[classFile.length()];
                                classStream.read
                                        ( classBytes, classFile.length() );
                                /* Not exactly sure on the args to this */
                                return defineClass( name, classBytes );
                        }
                }
                throw new ClassNotFoundException( name );
        }
}

Needs some more error-checking, but that's how it works.

--
Tom Cook - Software Engineer

"The brain is a wonderful organ. It starts functioning the moment you get
up in the morning, and does not stop until you get into the office."
        - Robert Frost

LISAcorp - www.lisa.com.au

--------------------------------------------------
38 Greenhill Rd.          Level 3, 228 Pitt Street
Wayville, SA, 5034        Sydney, NSW, 2000

Phone:   +61 8 8272 1555  Phone:   +61 2 9283 0877
Fax:     +61 8 8271 1199  Fax:     +61 2 9283 0866
--------------------------------------------------

Reply via email to