3 Replies Latest reply on Feb 3, 2008 4:06 PM by oskar.carlstedt

    reading a local properties file

    gryffin

      I'm writing a POJO/servlet based JAXWS web service on JBoss 4.0.5 w/ JBossWS 1.2.1. I'm trying to use a properties files located at /WEB-INF/properties.xml but I can't find the file. The only way I can seem to get the web service classes to find the properties file is to use an absolute URI from C: which is totally unworkable for production.

      Using this code:

      if ( !(new File(propertiesPath).exists()) )
       {
       log.fatal("NOT FOUND: " + propertiesPath );
       }
      


      This fails, I've tried variations, but can't seem to find the path:
      public static final String propertiesPath = "properties.xml";

      This works,
      public static final String queriesPath = "C:/Documents and Settings/tx714c/workspace/NlsAssociationService341/WEB-INF/properties.xml";

      and if I assume that '...\workspace' is my local dir, I can use a relative path. But I can't get a path local to the WAR file to work.

      Here's the listing of the war file:
      C:\Documents and Settings\tx714c\My Documents\workspace\ShimWS3.4.1>jar -tf gen\NlsAssociationServic
      e341.war
      META-INF/
      META-INF/MANIFEST.MF
      WEB-INF/
      WEB-INF/web.xml
      WEB-INF/classes/
      WEB-INF/classes/com/
      ...
      WEB-INF/classes/org/soapinterop/xsd/ValidationException.class
      WEB-INF/lib/
      ...
      WEB-INF/lib/wsdl4j-1.5.1.jar
      WEB-INF/properties.xml
      WEB-INF/queries.xml
      


      How do I access and load a local properties file from within the war? Thanks!



        • 1. Re: reading a local properties file
          peterj

          You should really place the properties file at WEB-INF/classes, then it should be easy to locate using Class.getResource() or ClassLoader.getResource() [simply supply the file name].

          • 2. Re: reading a local properties file
            gryffin

            Well, thank you Peter. Durn if that wasn't wasn't easy once it was explicitly laid out. I'd preferred to keep only classes in the classes directory, but I didn't see any working examples of this so I was stymied.

            Thanks again, Peter.

            • 3. Re: reading a local properties file
              oskar.carlstedt

              Hello!

              This is the way to go. Never add a properties-file in the WEB-INF folder. A better place is on the classpath, i.e. in the WEB-INF/classes directory. Then you can access this file with the following command:

              Thread.currentThread().getContextClassloader().getResourceAsStream("filename.extension");
              



              Cheers
              /Oskar