ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dave young <dyo...@bsis.com.au>
Subject problems loading resources within classpath
Date Mon, 30 Jul 2001 08:26:16 GMT
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