How do I Locate an application properties file

Discussion in 'Java' started by MattC, Sep 16, 2005.

  1. MattC

    MattC Guest

    I am working in a Struts web based J2EE environment. My application is
    packaged as a web app.

    I have application level configuration information that needs to be
    kept in a properties file. My problem is how to programmatically
    determine the absolute pathname of that properties file.

    It seems to me I have two basic options.

    1.) I can create the properties file and put it in a "known" location.
    For instance the WEB-INF\classes directory. In order to do this I will
    need to determine the absolute path name to the WEB-INF directory. For
    instance it might be C:\myproject\public_html\WEB-INF. How can I
    determine the absolute pathname of the WEB-INF directory?

    2.) I can store a relative pathname of the properties file in the
    web.xml file. This is a more flexible implementation and so it is
    preferred over option #1. In order to do this I need to determine the
    physical location of the WEB-INF directory as in option #1. In addition
    I need to gain access to the web.xml file.

    Can someone tell me how to access the web.xml file from a Struts
    application? In other words, I don't have access to the servlet.init()
    method and its associated ServletConfig object.

    Thanks,
    Matt
     
    MattC, Sep 16, 2005
    #1
    1. Advertising

  2. MattC

    Malte Guest

    MattC wrote:
    > I am working in a Struts web based J2EE environment. My application is
    > packaged as a web app.
    >
    > I have application level configuration information that needs to be
    > kept in a properties file. My problem is how to programmatically
    > determine the absolute pathname of that properties file.
    >
    > It seems to me I have two basic options.
    >
    > 1.) I can create the properties file and put it in a "known" location.
    > For instance the WEB-INF\classes directory. In order to do this I will
    > need to determine the absolute path name to the WEB-INF directory. For
    > instance it might be C:\myproject\public_html\WEB-INF. How can I
    > determine the absolute pathname of the WEB-INF directory?
    >
    > 2.) I can store a relative pathname of the properties file in the
    > web.xml file. This is a more flexible implementation and so it is
    > preferred over option #1. In order to do this I need to determine the
    > physical location of the WEB-INF directory as in option #1. In addition
    > I need to gain access to the web.xml file.
    >
    > Can someone tell me how to access the web.xml file from a Struts
    > application? In other words, I don't have access to the servlet.init()
    > method and its associated ServletConfig object.
    >
    > Thanks,
    > Matt
    >


    Below is an outlined example. The idea is to use the webapp.xml file.
    Put in a context-param tag, then in your Java code, use the servlet
    context to retrieve the information. If you, like I did for the example,
    stuff your properties file into /WEB-INF/data, getRealPath() will find
    it for you nicely. What you don't want to do is hardcode c:\ or
    something ugly. What if someone deploys your application to a real OS
    when it would become something line /opt/mywebapps ?

    <web-app>

    <context-param>
    <param-name>DATA_DIR</param-name>
    <param-value>/WEB-INF/data</param-value>
    </context-param>
    ...
    </web-app>

    private File getDataDir() throws ServletException {
    ServletContext application = getServletContext();
    String dataDir = application.getInitParameter(DATA_DIR_PARAM);

    if (dataDir == null || dataDir.length() == 0)
    error(FILE_COMP_ID, MISSING_DATA_DIR_PARAM_ERROR,
    FacesMessage.SEVERITY_FATAL);

    String realDir = application.getRealPath(dataDir);
    if (realDir == null)
    error(FILE_COMP_ID, CANNOT_ACCESS_DATA_DIR_ERROR,
    FacesMessage.SEVERITY_FATAL);

    return new File(realDir);
    }
     
    Malte, Sep 16, 2005
    #2
    1. Advertising

  3. MattC

    Roedy Green Guest

    On 16 Sep 2005 14:24:48 -0700, "MattC" <>
    wrote or quoted :

    >I have application level configuration information that needs to be
    >kept in a properties file. My problem is how to programmatically
    >determine the absolute pathname of that properties file.


    Frustrating eh? especially if you want this to work muliplatform.

    Look into the Preferences API for getting around that problem.
    The other way is to use JAWS.

    see http://mindprod.com/jgloss/preferences.html
    http://mindprod.com/jgloss/avawebstart.html

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 17, 2005
    #3
  4. MattC wrote:
    > I am working in a Struts web based J2EE environment. My application is
    > packaged as a web app.
    >
    > I have application level configuration information that needs to be
    > kept in a properties file. My problem is how to programmatically
    > determine the absolute pathname of that properties file.


    Why do you need the absolute pathname? The simplest solution is to put
    the .properties file in the classpath and load it via ResourceBundle.
    If you do not want a ResourceBundle you can use
    ClassLoader.getResource() to get the URL.

    >
    > It seems to me I have two basic options.
    >
    > 1.) I can create the properties file and put it in a "known" location.
    > For instance the WEB-INF\classes directory. In order to do this I will
    > need to determine the absolute path name to the WEB-INF directory. For
    > instance it might be C:\myproject\public_html\WEB-INF. How can I
    > determine the absolute pathname of the WEB-INF directory?


    ServletContext or ServletConfig has a getRealPath() method that can help
    you. However, there is no real need to do it this way.

    >
    > 2.) I can store a relative pathname of the properties file in the
    > web.xml file. This is a more flexible implementation and so it is
    > preferred over option #1. In order to do this I need to determine the
    > physical location of the WEB-INF directory as in option #1. In addition
    > I need to gain access to the web.xml file.
    >
    > Can someone tell me how to access the web.xml file from a Struts
    > application? In other words, I don't have access to the servlet.init()
    > method and its associated ServletConfig object.
    >


    Look into just putting it in the classpath. It's the easiest way and it
    works in every environment (web container, EJB, applet, etc.).


    HTH,
    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Sep 17, 2005
    #4
  5. MattC

    Roedy Green Guest

    On Sat, 17 Sep 2005 22:20:23 GMT, Raymond DeCampo
    <> wrote or quoted :

    >Why do you need the absolute pathname? The simplest solution is to put
    >the .properties file in the classpath and load it via ResourceBundle.
    >If you do not want a ResourceBundle you can use
    >ClassLoader.getResource() to get the URL.


    If it is a read-only properties file, you can put it in the jar. The
    problem only comes when you have to create a properties file out of
    thin air and it is not clear where a safe place to put it is.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    461
    John Timney \(ASP.NET MVP\)
    Apr 8, 2005
  2. Reggie

    Locate File Diologe

    Reggie, Jun 17, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    966
    Reggie
    Jun 18, 2005
  3. shawnk
    Replies:
    0
    Views:
    416
    shawnk
    Apr 29, 2005
  4. vsoler
    Replies:
    4
    Views:
    2,303
    vsoler
    Aug 4, 2010
  5. sp1dEr
    Replies:
    0
    Views:
    148
    sp1dEr
    Aug 1, 2005
Loading...

Share This Page