tomcat + java.io.FileNotFoundException

Discussion in 'Java' started by Victoria, Apr 8, 2004.

  1. Victoria

    Victoria Guest

    Dear all,

    I use Tomcat to display a jsp page.
    In this jsp page, I call a java program.
    In this java program, I write the following code :
    File f_keyword = new File ( "cat1.txt" );

    In jsp page, exception occur : java.io.FileNotFoundException: cat1.txt
    The system cannot find "cat1.txt" file.
    However, I 've put cat1.txt and java program in the WEB-INF\classes directory.
    I try to put "cat1.txt" in the jsp page directory, but the system still cannot find.

    I try to use full path and the system can find "cat1.txt" file.
    But can I use short path ? If yes, where should I put the "cat1.txt" file?

    Thanks for answering my question.
    Victoria
     
    Victoria, Apr 8, 2004
    #1
    1. Advertising

  2. On 8 Apr 2004 12:01:42 -0700, Victoria wrote:

    > Dear all,
    >
    > I use Tomcat to display a jsp page.
    > In this jsp page, I call a java program.
    > In this java program, I write the following code :
    > File f_keyword = new File ( "cat1.txt" );


    // ** in the JSP
    // at the root of the site
    File f1 = new File(request.getRealPath("/cat1.txt"));

    // the same directory as the JSP
    File f1 = new File(request.getRealPath("cat1.txt"));

    Then call your other class using that
    file as a parameter..

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Apr 8, 2004
    #2
    1. Advertising

  3. Victoria

    Chris Smith Guest

    Victoria wrote:
    > I use Tomcat to display a jsp page.
    > In this jsp page, I call a java program.
    > In this java program, I write the following code :
    > File f_keyword = new File ( "cat1.txt" );


    To expand on Andrew's response, the constructor for the java.io.File
    class interprets relative paths as being relative to something called
    the "current working directory". The "current working directory" is a
    concept that's quite useful for command-line applications, but next to
    useless for GUI applications or stand-alone server-side code such as a
    web server. As a result, unless you happen to be developing a command-
    line application, you should avoid ever using a relative file path as
    the single parameter to a constructor for java.io.File. You should also
    avoid anything else that depends on the current working direction, such
    as using the user.dir system property, or displaying a JFileChooser
    without setting its current directory first.

    Otherwise, you run into situations like this where you just assumed that
    the "current working directory" is one thing, when in reality it's
    something completely different.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Apr 8, 2004
    #3
    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. Brian
    Replies:
    1
    Views:
    3,279
    Rick Strahl [MVP]
    Dec 5, 2003
  2. Stephen Witter
    Replies:
    2
    Views:
    5,470
    Stephen Witter
    May 28, 2004
  3. msnews.microsoft.com

    System.IO.FileNotFoundException help

    msnews.microsoft.com, Jun 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,224
    msnews.microsoft.com
    Jun 3, 2004
  4. apatruduque
    Replies:
    3
    Views:
    9,929
    apatruduque
    Sep 14, 2004
  5. bond
    Replies:
    1
    Views:
    3,209
    Mark Jeffcoat
    Jun 28, 2007
Loading...

Share This Page