aspose file tools*
The moose likes Servlets and the fly likes FileNotFound Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "FileNotFound" Watch "FileNotFound" New topic
Author

FileNotFound

Eric Racin
Greenhorn

Joined: Oct 13, 2010
Posts: 12
On my test Server 2003, the Servlet successfully reads files from the Tomcat\bin directory (files read from init() method).
On the test server, the Servlet works but fails to read the files (throws FileNotFound exception).
Any ideas?
Ed Ward
Ranch Hand

Joined: Jan 30, 2006
Posts: 147
Path or permissions?
From FileNotFoundException API(SE6):
This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

TellTheDetails


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

Yes please tell the details. Without details it is difficult to know
Eric Racin
Greenhorn

Joined: Oct 13, 2010
Posts: 12
Tomcat message:

Oct 15, 2010 12:48:55 PM org.apache.coyote.http11.Http11BaseProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-80
Starting service Tomcat-Standalone
Apache Tomcat/4.1.36
Oct 15, 2010 12:48:57 PM org.apache.coyote.http11.Http11BaseProtocol start
INFO: Starting Coyote HTTP/1.1 on http-80
Oct 15, 2010 12:48:57 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Oct 15, 2010 12:48:57 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/32 config=null
Can't open carbon.oak Error: java.io.FileNotFoundException: carbon.oak (The system cannot find the file specified)
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Hello, Eric Racin

The error message says that the tomcat can't find "carbon.oak". Does your app use this file?

I've searched at web and it looks like it is not related with tomcat.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Eric Racin
Greenhorn

Joined: Oct 13, 2010
Posts: 12
Yes, carbon.oak is the file I am trying to open. I placed it in
\Tomcat\bin
\Tomcat\webapps\ROOT
\Tomcat\webapps\carbonapp3\WEB-INF\classes
and the Servlet can't find open the file.
Is there anywhere else the Servlet could be looking?
What is strange that it is opened fine in a similar Windows 2003 Server environment.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

Why are you guessing like that? Here's what you should do.

(1) Choose a directory and put the file there.
(2) Provide the full path to that directory to whatever is trying to open it.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Could you post the code that reads the file?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16141
    
  21

If you're attempting to reference the Tomcat binary directory on Linux, Solaris, AIX, or, well, any OS but Windows, it's going to fail if you say "tomcat\bin". Backslash separators ONLY work for Windows, and even there, they can be pretty dangerous when working with Java - you're better off saying "tomcat/bin", which works everywhere, including Windows.

However, I don't encourage putting user files in Tomcat's bin directory. Pick a place that isn't dedicated to a private purpose already.

Incidentally, if you want to read a file, don't plan on writing to the file and the file isn't expected to change, you can include it as a resource in your WAR and use the request getResourceAsStream to open an input stream on it.

One thing you should NEVER do is have a webapp write files into WARs, however!


Customer surveys are for companies who didn't pay proper attention to begin with.
Eric Racin
Greenhorn

Joined: Oct 13, 2010
Posts: 12
I am using Server 2003. This is the the syntax:
BufferedReader is = new BufferedReader(new FileReader(sCarbonFile));

If I want to designate a directory, as you imply, would the syntax be:
BufferedReader is = new BufferedReader(new FileReader(/Tomcat/dir/sCarbonFile));
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16141
    
  21

Eric Racin wrote:I am using Server 2003. This is the the syntax:
BufferedReader is = new BufferedReader(new FileReader(sCarbonFile));

If I want to designate a directory, as you imply, would the syntax be:
BufferedReader is = new BufferedReader(new FileReader(/Tomcat/dir/sCarbonFile));


Better to say "C:/Tomcat/dir/sCarbonFile". Otherwise the path is ambiguous.
Shaaf Shah
Greenhorn

Joined: Oct 21, 2010
Posts: 2
I think this is what you should do

SOURCE : http://www.exampledepot.com/egs/javax.servlet/GetInit.html
Under servlet class add this


And read the parameter from the file as mentioned in the source.

This way you dont need to change your code file every time just change it in the XML where ever you take it.

Hope that helps!


// Shaaf
Chetan Natu
Greenhorn

Joined: Dec 03, 2005
Posts: 19
Hi Eric,

I am going to provide you with a totally different solution than any which has been discussed here.

If I read correctly, you need to read a file into your web application and you are going to port the file along with the application wherever you host it. First and foremost do not use hard-coded absolute path, as has been suggested by some. This will tie your application down to the exact directory structure and becomes useless on any other kind of requirement.

To open a file you are going to need absolute path to the file so we go about getting the absolute path (sounds contradictory , read on) without having to change the code or disturbing the application.

There is a method in ServletContext interface called getRealPath this method coverts the virtual path of the application into an absolute path depending on the App Server + OS at runtime. So regardless of where you have deployed the application, this method will always give you the absolute path to the context of your web app, plus you don't even have to bother about confusion of slash (/) or backslash (\). This method returns a string, which is appropriate for your situation.

So get the real path to the context, attach the filename (with subdirectories if that applies) and then open this using the normal IO methods.

Hope that helps


Never Remember the failure
Never Forget the mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FileNotFound