ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Filip Cruz <fc...@camail.harvard.edu>
Subject Re: problems loading resources within classpath
Date Mon, 30 Jul 2001 13:36:46 GMT
Yes, I have seen the same behavior. The fix is to fork the process and it 
has worked fine for me. Now if I could just get JUnit to do the same...oops 
that's another mailing list. Sorry.

At 04:56 PM 7/30/2001 +0800, you wrote:
>After a couple more tests, it seems that as long as the directory
>containing the file is in the CLASSPATH when ant is started, that the
>resource will be found.
>
>Another test has shown that if I set fork="yes" in the <java> task, that
>the class loader seems to behave itself a bit better.
>
>regards,
>
>Dave Young.
>
>Still, it would be nice not to have this requirement, we prefer to
> >
> > I have been experiencing difficulties whilst attempting to load system
> > resources within ant.
> >
> > The following java code illustrates what we are attempting to do;
> >
> > ----------------------< cut here >-----------------------
> > import java.util.*;
> > import java.io.*;
> >
> > public class p45
> > {
> >
> > public static void main( String[] argv )
> > {
> >         loadProperties();
> >         System.out.println( "Hello World!!" );
> > }
> >
> >   protected static Properties loadProperties()
> >     {
> >         try
> >         {
> >             // get the properties file from the classpath.
> >             Properties p = new Properties();
> >             p.load( ClassLoader.getSystemResourceAsStream(
> > "my.properties" ) );
> >
> >             // dump out the properties
> >             StringWriter sw = new StringWriter();
> >             PrintWriter pw = new PrintWriter(sw);
> >             p.list(pw);
> >             System.out.println( "Loaded  properties file\n" +
> > sw.toString() );
> >
> >             return p;
> >         } catch( Exception e ) {
> >             System.out.println( "Caught " + e.getClass().getName() + " -
> > " + e.getMessage() );
> >             System.out.println( "Could not load properties file." );
> >             throw new RuntimeException( "Could not load properties
> > file." );
> >         }
> >     }
> > }
> > ----------------------< cut here >-----------------------
> >
> > we find that this java code works just fine when not run from within ant
> > (ie: java -classpath <your-classpath-goes-here> p45), as long as the
> > file "my.properties" lies within a directory within the classpath of the
> > java program running it.
> >
> > when we try to run the same java code from within ant, being especially
> > careful that the directory containing the file is in our classpath, we
> > find that the file is not loaded and a NullPointerException is thrown
> > (because the Properties.load() method is expecting a valid input
> > stream).
> >
> > does ant do bizarre things to the class loading that would affect this
> > sort of behaviour? we find that loading resources from the class path is
> > especially useful, since we run on both windoze and slowlaris platforms,
> > so not hardcoding directory paths is definitely a good thing for us.
> >
> > hope somebody can help us out.
> >
> > best regards,
> >
> > dave young.



Mime
View raw message