File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNotFound

 
Eric Racin
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
 
Anupam Dee
Ranch Hand
Posts: 42
Firefox Browser Tomcat Server Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes please tell the details. Without details it is difficult to know
 
Eric Racin
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Racin
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the code that reads the file?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17646
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Eric Racin
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17646
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chetan Natu
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic