-
1. Re: Finding a file
jonlee Nov 21, 2003 3:20 AM (in response to katsteve)You should use getResourceAsStream. For example one of my MBeans reads some configuration information from META-INF/keys.xml located in the JAR the MBean class is in.
InputStream publicKeyStream = this.getClass().getClassLoader().getResourceAsStream("META-INF/keys.xml");
Sometimes, you can get an incomplete read of the file using the standard InputStream and what you read into the buffer is less than what is available in the stream - .available() > .read(buffer, 0, .available()). If this happens, wrap it in a BufferedInputStream. -
2. Re: Finding a file
katsteve Nov 21, 2003 1:05 PM (in response to katsteve)Thanks. I was expecting some sort of JNDI answer, but I guess the root directory is based on the ClassLoader, huh.
My server has gone to the shop for a week, so I can't test it until it returns. I'll write again if it doesn't work.
Thanks again.
Steve -
3. Re: Finding a file
jonlee Nov 21, 2003 4:10 PM (in response to katsteve)I suppose you could do it via JNDI but normally, you'd reserve that for things that global components need to share.
One other thing: servlets have a ServletContext.getResourceAsStream() method as well, which reads from the servlet's context, and prevents you from going to some other place outside your context. So if you want to get something from the WAR WEB-INF directory, you have to append a '/'.
e.g. stream = context.getResourceAsStream("/WEB-INF/mykeys.xml");