3 Replies Latest reply on Nov 21, 2003 4:10 PM by jonlee

    Finding a file

    katsteve

      My app uses a config xml file that is deployed along with it in its jar. How do I config jboss to tell the app where the file is located?

      Along the same lines -- is the java:/comp/env namespace required? If not, where is it changed?

      TIA,
      Steve

        • 1. Re: Finding a file
          jonlee

          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

            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

              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");