Hi Fredrick,

 

FileInputStream works with file paths, which have no relationship to your classpath.  Relative file paths are resolved against the user’s current directory.  You want:

 

url = getResource("/applicationContext.xml");

inputStream = url.openStream();

 

This will open a file called applicationContext.xml occurring directly under any directory in your classpath.

 

Cheers,

Dan

 


From: beanshell-users-admin@lists.sourceforge.net [mailto:beanshell-users-admin@lists.sourceforge.net] On Behalf Of frederick.verbist@steria.be
Sent: Wednesday, August 17, 2005 6:38 AM
To: beanshell-users@lists.sourceforge.net
Subject: [Beanshell-users] Loading resource from classpath

 


Hi,

I would like to read a file from the classpath.
I add the the directory to the classpath but get a FileNotFoundException when trying to open it :-(
I tryed with both beanshell 1.3 and 2.0

here is my code :

bsh % print(getClassPath());
java.net.URL[]: {
file:/C:/java/beanshell-1.3/bsh-1.3.0.jar,
file:/C:/java/beanshell-1.3/,
file:/C:/java/beanshell/bsh-2.0b4.jar,
file:/C:/Program Files/Java/jdk1.5.0_04/lib/tools.jar,
file:/C:/Program Files/Java/jre1.5.0_04/lib/rt.jar,
file:/C:/Documents and Settings/xxx/src/conf/,
}
bsh % dir ("C:/Documents and Settings/xxx/src/conf");
rw_ Aug 16     3221 applicationContext.xml
rw_ Aug 16      387 jdbc.properties
rw_ Aug 12      376 log4j.properties
rw_ Aug 16      590 persistence.hbm.xml
bsh % b = new FileInputStream("applicationContext.xml");
// Error: // Uncaught Exception: Object constructor : at Line: 15 : in file: <unknown file> : new FileInputStream ( "applicationContext.xml" )

Target exception: java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)

java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at bsh.Reflect.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.constructObject(Unknown Source)
        at bsh.BSHAllocationExpression.objectAllocation(Unknown Source)
        at bsh.BSHAllocationExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHPrimaryExpression.eval(Unknown Source)
        at bsh.BSHAssignment.eval(Unknown Source)
        at bsh.Interpreter.run(Unknown Source)
        at bsh.Interpreter.main(Unknown Source)
bsh %

what am I doing wrong?

thanks,

Fred