You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Jacques Bergeron <ja...@dogico.com> on 2000/06/15 02:33:53 UTC

Dynamic load of Ant [EXPIRED TRIAL LICENCE]

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 :)



Jacques




Re: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

Posted by Peter Donald <do...@mad.scientist.com>.
At 12:31  15/6/00 +0930, you wrote:

>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 
...snip...

This is from memory but 1.2 classloaders are as easy as pie ... simply go

URL urls[] = { new URL( "file:" + path ) };
ClassLoader cl = new java.net.ClassLoader( urls );

Done ! :P

Cheers,

Pete

*------------------------------------------------------*
| "Nearly all men can stand adversity, but if you want |
| to test a man's character, give him power."          |
|       -Abraham Lincoln                               |
*------------------------------------------------------*

Re: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

Posted by Tom Cook <tc...@ardec.com.au>.
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
--------------------------------------------------


RE: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

Posted by Jacques Bergeron <ja...@dogico.com>.
Hi Kevin, all

Works fine!

Thanks a lot. I will correct the guys at Java Developper Connection that
said URLClassLoader does not support file: protocol. You know better!

Regards,

Jacques

-----Message d'origine-----
De : Kevin Regan [mailto:kevinr@valicert.com]
Envoyé : 14 juin, 2000 23:13
À : ant-dev@jakarta.apache.org
Objet : Re: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

What I've been doing:

     private Class getClassFromClasspath( String classname,
                                                 String classpath )
         throws EventException, ClassNotFoundException {

         URL[] urls = parseClasspath( classpath );

         URLClassLoader loader = new URLClassLoader( urls );

         return loader.loadClass( classname );

    } // getClassFromClasspath


    private URL[] parseClasspath( String classpath )
        throws EventException {

        List urls = new ArrayList();

        StringTokenizer tokenizer =
            new StringTokenizer( classpath, CLASSPATH_SEPARATOR );

        while ( tokenizer.hasMoreTokens() ) {
            String filename = tokenizer.nextToken();

            File file = new File( filename );

            try {
                urls.add( file.toURL() );
            }
            catch ( MalformedURLException e ) {
                throw new EventException( "invalid filename: \"" +
filename +
                                          "\"" );
            }
        }

        if ( urls.size() == 0 ) {
            throw new EventException( "no paths found in \"" + classpath +
                                      "\"" );
        }

        return ( URL[] ) urls.toArray( new URL[ urls.size() ] );

    } // parseClasspath



On Wed, 14 Jun 2000, Kevin Regan wrote:

>
> Hmm, I've been usingthe URLClassLoader with files for a long
> time now.  Try it...
>
> --Kevin
>
>
>
> 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 :)
> >
> >
> >
> > Jacques
> >
> >
> >
>



Re: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

Posted by Kevin Regan <ke...@valicert.com>.

What I've been doing:

     private Class getClassFromClasspath( String classname,
                                                 String classpath )
         throws EventException, ClassNotFoundException {

         URL[] urls = parseClasspath( classpath );

         URLClassLoader loader = new URLClassLoader( urls );

         return loader.loadClass( classname );

    } // getClassFromClasspath
                                    

    private URL[] parseClasspath( String classpath )
        throws EventException {

        List urls = new ArrayList();

        StringTokenizer tokenizer =
            new StringTokenizer( classpath, CLASSPATH_SEPARATOR );

        while ( tokenizer.hasMoreTokens() ) {
            String filename = tokenizer.nextToken();

            File file = new File( filename );

            try {
                urls.add( file.toURL() );
            }
            catch ( MalformedURLException e ) {
                throw new EventException( "invalid filename: \"" +
filename +
                                          "\"" );
            }
        }

        if ( urls.size() == 0 ) {
            throw new EventException( "no paths found in \"" + classpath +
                                      "\"" );
        }

        return ( URL[] ) urls.toArray( new URL[ urls.size() ] );

    } // parseClasspath
      


On Wed, 14 Jun 2000, Kevin Regan wrote:

> 
> Hmm, I've been usingthe URLClassLoader with files for a long
> time now.  Try it...
> 
> --Kevin
> 
> 
> 
> 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 :)
> > 
> > 
> > 
> > Jacques
> > 
> > 
> > 
> 


Re: Dynamic load of Ant [EXPIRED TRIAL LICENCE]

Posted by Kevin Regan <ke...@valicert.com>.
Hmm, I've been usingthe URLClassLoader with files for a long
time now.  Try it...

--Kevin



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 :)
> 
> 
> 
> Jacques
> 
> 
>