File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Problem reading file: Bad file descriptor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Problem reading file: Bad file descriptor" Watch "Problem reading file: Bad file descriptor" New topic

Problem reading file: Bad file descriptor

Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342

I have a servlet running under Tomcat 5.0.28 which reads data from a large binary file on disk. Problem is I am seeing a Bad file descriptor when I try to use call.

I'm at a bti of a loss on this one. A stand alone app which does the same thing on the same PC reading from the same data file accesses it no problem. I'd be grateful of any suggestions, especially from anyone who has seen this error before under Tomcat. I have experienced something similar before but I cannot remember how it was resolved


SCJP 1.4,
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

How are you locating the file on the filesystem?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
The file locations are being read from a database table, some are in the form:


Others are network paths like:


Neither seem to work, although they do work when used with a stand-alone app.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Turned out to be a premature close() call on RandomAccessFile.
John Te

Joined: Jun 07, 2010
Posts: 1

I also run the same problem today.

Platform : MAC OS with Java 1.6, Tomcat 6.x --

Purpose: Try to create a new file and write new file content inside Tomcat servlet.

Exception in Tomcat: Bad file descriptor
at Method)
at sun.nio.cs.StreamEncoder.writeBytes(
at sun.nio.cs.StreamEncoder.implClose(
at sun.nio.cs.StreamEncoder.close(

// write to the same file
File fh2 = null;
try {
fh = resource.getFile();
fh2 = new File(fh.getAbsolutePath() + ".new");
os = new FileOutputStream(fh2);
bw = new BufferedWriter(new OutputStreamWriter(os));
for (int i = 0; i < list.size(); i++) {
} catch (Exception e) {
throw e;
} finally {
try {
if (os != null) {

I agree. Here's the link:
subject: Problem reading file: Bad file descriptor
jQuery in Action, 3rd edition