#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Boston, MA
    Posts
    48
    Rep Power
    15

    Question How to get context path for new File()?


    Hi,

    I have a problem. One of my methods needs to pull files from certain directory and for that I use:

    PHP Code:
    File directory = new File("c:\\apps\\project\\build\\WEB-INF\\classes\\sql"); 
    However I'd like to replace the OS dependent part of this path with context or relative path. From where in Java/Tomcat can I get the current context path?

    Thanks,
    goe
    Java, PHP, Perl, ASP, MySQL, Oracle -> CodeFunctions.com
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    122
    You can use several ways to get a resource on the file system. ServletContext.getRealPath allows you to create a path to a resource in the web app.

    Another way (arguably better), is to use ServletContext.getResource and getResourceAsStream methods.

    Comments on this post

    • Yawmark agrees
    ~Nemi

    Before posting did you try:
    [ Javadocs | Google ]

IMN logo majestic logo threadwatch logo seochat tools logo