3 Replies Latest reply on Feb 11, 2005 12:29 PM by Franco

    Unable to load .properties file

    James Adams Novice

      I have a JBoss application which is deployed in its own directory, for example JBOSS_HOME/server/myapp/deploy/myapp.sar. In this directory is a myapp.properties file. I have been unsuccessful at loading the Properties contained in this file, and I can't figure out why. First I tried a simple load using a FileInputStream, for example:

      String propertiesFileName = "myapp.properties";
      Properties myProperties;
      myProperties.load(new FileInputStream(propertiesFileName));
      


      I have also tried the following:

      String propertiesFileName = "myapp.properties";
      Properties myProperties;
      InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream(propertiesFileName);
      myProperties.load(inputStream );
      



      The first example resulted in a FileNotFoundException, and the second results in a NullPointerException.

      In the run.sh I use to start JBoss I have made sure that the application's directory is in the $JBOSS_CLASSPATH, in order to make the myapp.properties file easy to locate.

      I am using JBoss 4.0.1.

      Thanks in advance for any suggestions as to how I might make this work.


      --James

        • 1. Re: Unable to load .properties file
          Franco Apprentice

          Hi,
          try:
          PropertyResourceBundle prs = new PropertyResourceBundle( Thread.currentThread( ).getContextClassLoader( ).getResourceAsStream( "my.properties" ) );

          or add a / on the file name if it is on the files system (bin directory).

          • 2. Re: Unable to load .properties file
            James Adams Novice

            Thanks so much, getting a PropertyResourceBundle as you describe works well. The initial slash in front of my properties file name didn't help. It seems that loading Properties isn't as straight-forward as it should/could be with JBoss. Can it even be done at all ? If so can someone post the code or the configuration required ?


            --James

            • 3. Re: Unable to load .properties file
              Franco Apprentice

              Hi,
              try this (file in /jboss/bin/):

              ClassLoader loader = Thread.currentThread().getContextClassLoader();
              InputStream is = loader.getResourceAsStream("my.properties");

              or:

              java.net.URL url =
              ClassLoader.getSystemResource("my.properties");
              myProps = new java.util.Properties();
              myProps.load(url.openStream());