File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes File listing not working in Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "File listing not working in Tomcat" Watch "File listing not working in Tomcat" New topic

File listing not working in Tomcat

Gabriel Solano

Joined: Jul 19, 2010
Posts: 5
This is the first time I post a question here. Sorry if don't follow a specific convention.
I have this code that is working fine when I run it as a Java application (stand alone). It listes the files fine.
But when I try to run it in my Web App in Tomcat, the files are never listed because the remote folder does not exist.

Is there a restriction in Tomcat to open remote network addresses? The folder I am trying to open is in a different network. Again, the strange thing is that in a normal command simple program, the code runs fine. My issue is with my web app. Thanks!
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63870

Are you expecting the output to appear in the browser? If so, you are definitely way off track.

How are you even running a Java program with a main() "in Tomcat"?

[Asking smart questions] [About Bear] [Books by Bear]
Gabriel Solano

Joined: Jul 19, 2010
Posts: 5
Absolutly not. The code I want to run does not include the prints. This is an example of the code I'm running in my Struts web app (a class called by an action). It's a recursive scanning using file.listFiles(). The problem is that when I try to open the remote address, the folder is not found. But if I do it in a normal Java application, the folder is opened fine. Hope it clarifies!

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Your directory path is what's technically referred to as a "UNC name", consisting of a servername plus a sharename. It's a construct unique to Windows fileshares. On a Linux/Unix system using Samba, the format would be "//hostname/sharename", which is a safer from accidents that come from miscounting backslash escapes.

It's possible, I suppose, that Tomcat is sandboxing UNC paths. However it's not common to use UNC paths that way in any event. Although there is a general-purpose apache commons library that can handle this kind of stuff, for a server, you'd usually want a more permanent and more traditional mechanism: a filesystem mount.

For Windows: "NET USE X: \\hostname\sharename", where X is any suitable drive ID.

For Linux use smbmount for a cifs node - usually automounted. For Solaris, you have to purchase a third-party package to make SMB/CIFS shares mountable, as the Solaris version of Samba lacks the smbmount feature.

There are 2 benefits to permanently mounting a share. The first one is that you can now access the share and its children just like any other local filesystem node, allowing for the variances in behaviour that come when you have different security systems, etc. involved. The second one is that you don't put the system through the overhead of resolving the network share every time you want to open a file, list a directory, or whatever. It's already resolved and locked down.

An IDE is no substitute for an Intelligent Developer.
Gabriel Solano

Joined: Jul 19, 2010
Posts: 5
Actually, I tried that before, mounting a network drive, but it was the same as using the UNC path. The remote server is located in a Windows and requires an user and password authentication which is already stored in the WIN 2003 server where I have the Tomcat. Maybe there is something in the server's configuration that restricts any web page to open a connection to a different network.
Gabriel Solano

Joined: Jul 19, 2010
Posts: 5
Solved!! It was indeed a permissions issue. Tomcat needed to start as the user I used to access the remote address. It was a matter of configuring the "Log On" tab in the Tomcat service.

I agree. Here's the link:
subject: File listing not working in Tomcat
It's not a secret anymore!