4 Replies Latest reply on Nov 21, 2006 3:52 AM by prakash govindarajan

    How to access property  files

    prakash govindarajan Newbie

      Hi ,
      I am using jboss with eclipse IDE, I clouldn't access the property files even if it is in the calss path, but it woks if the propery file is in web-inf classes folder.
      Kinldy help me to understand what happens

        • 1. Re: How to access property  files
          jaikiran pai Master

          When you say, you had placed the properties file in your classpath, where exactly had you placed that? Also how are you trying to access the file in your code? Please post the code.

          • 2. Re: How to access property  files
            prakash govindarajan Newbie

            private static LoadConfig loadConfig = new LoadConfig();
            private Class ldClass = null;
            private final String PROP_FILE_NAME = "config.properties";
            private Properties prop = new Properties();
            private LoadConfig()
            {
            try {
            prop.load(Thread.currentThread().getContextClassLoader()
            .getResourceAsStream(PROP_FILE_NAME));
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            }

            public String getFilePath()
            {
            return this.prop.getProperty("XML_PATH");
            }

            public String getResultPath()
            {
            return this.prop.getProperty("RESULT_PATH");
            }

            public static LoadConfig getConfig(){
            return loadConfig;
            }

            This is the code , I added the property file to the class path by editing configure buil path tab in eclipse, thanks for your reply

            • 3. Re: How to access property  files
              jaikiran pai Master

               

              prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(PROP_FILE_NAME));


              I usually do it this way, whenever i am loading a properties file:

              prop.load(this.getClass().getClassLoader().getResourceAsStream(PROP_FILE_NAME));


              Assuming that the PROP_FILE_NAME is just the name of the file and not the path, then that file should be present directly inside the war/ear/jar. If the PROP_FILE_NAME is something like "/conf/myprops/someprop.properties" then this is how the directory structure will look like:

              myApp.war
               |
               |------------ META-INF
               |
               |
               |------------- conf
               | |------ myprops
               | |------------ someprop.properties
               |
               |------------- WEB-INF
               | |--------- classes
              



              • 4. Re: How to access property  files
                prakash govindarajan Newbie

                Thanks a lot now the property file gets loaded ....