tomcat + java.io.FileNotFoundException

V

Victoria

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
 
A

Andrew Thompson

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..
 
C

Chris Smith

Victoria said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top