aspose file tools*
The moose likes Servlets and the fly likes Having problems reading a file from servlet in Windows XP with Tomcat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Having problems reading a file from servlet in Windows XP with Tomcat" Watch "Having problems reading a file from servlet in Windows XP with Tomcat" New topic
Author

Having problems reading a file from servlet in Windows XP with Tomcat

Thad Henry
Greenhorn

Joined: Sep 18, 2003
Posts: 5
I am running into a situation where I am not sure where to go next and things are driving me crazy. I have a servlet in Tomcat that has a startup parameter that tells me the location of my properties file. I can retrieve this parameter and SOP it and looks fine.
I then try and read the file into the properties and I get an error stating that the file was not found. I have checked the file and it is there and it is in a simple directory called c:\\test, so the 8.3 is not a problem. I have written and re-written this many different ways and all say the same thing "File not found"
I can however take the same directory and create a file there, and that works. I really think it has to do with XP but want to see if anyone can help.
I will keep up to date on this through out the day and reply if I can. Thanks in advance for the help
[ September 18, 2003: Message edited by: Thad Henry ]

***********************************<br />There are only 10 kinds of people in the world.<p>Those that understand binary, and those that don't.....<p>***********************************
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Well, could you show us the code where you are trying to load the file?
Also, is there a reason you have this properties file on the root of your C drive?
What I have seen done, and what I tested and it worked, is putting a properties file in the WEB-INF and then using the ServletContext to load the file. This is really easy and it puts your properties file in a protected space.


GenRocket - Experts at Building Test Data
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12783
    
    5
When file reading gives me a problem I like to create a java.io.File object and do a System.out.println( theFile.getAbsolutePath() );
This has revealed some odd naming problems.
Also, are you sure the file security clearances are ok?
Bill
Thad Henry
Greenhorn

Joined: Sep 18, 2003
Posts: 5
Everyone,
At this moment I don't have the code that I wrote ( I'm at work and code is at home), I will post as soon as I get home. However, I can answer a couple of questions. I put the file on the C drive to try an elminate any 8.3 issues (if there were any). I initially had some code that used the ServletContext to determine the file name and that is when the problems started. I had the file under the app folder in Tomcat in its own properties directory, and it could not find it, so I moved it to the root directory to help eliminate problems. This however did not help either.
I have done this type of code before but never on an XP machine, and I am wondering if there is some kind of security problem. My next step when I get home tonight is to transfer the code to a 98 machine running Tomcat and see if the problem still exists. I would have done it last night but it was 1:00am in the morning and work comes early.
William if you could let me know what security issues you have seen I would appreciate it. I am really new to XP and appreciate the help. Again thanks to all for the replies
Thad Henry
Greenhorn

Joined: Sep 18, 2003
Posts: 5
Everyone,
I found the code that I originally started out with, it was from the Log4J manual:
public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
I had the file in the WEB-INF/classes directory and it too could not find it. Just wanted to post an update.
Thad Henry
Greenhorn

Joined: Sep 18, 2003
Posts: 5
Everyone,
Here is the code that I am using and the output from the stdout. I have verified that the file is where it says it should be. Any help is greatly appreciated.
Code:
public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
Properties props = new Properties();
try {
System.out.println("File path is --> " + prefix + file);
FileInputStream inputStream = new FileInputStream(prefix + file);
props.load(inputStream);
System.out.println("log4j.rootLogger is --> " + props.get("log4j.rootLogger"));
} catch (Exception e) {
System.out.println("Exception in props load " + e);
}
System.out.println("The log config file is --> " + file);
try {
// if the log4j-init-file is not set, then no point in trying
if (file != null) {
//PropertyConfigurator.configure(props);
}
} catch (Exception e) {
System.out.println("Exception on property configurator " + e);
}

Output:
Bootstrap: Create Catalina server
Bootstrap: Starting service
Starting service Tomcat-Standalone
Apache Tomcat/4.1.27
File path is --> C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\marmon\\\properties\\log4j.lcf
Exception in props load java.io.FileNotFoundException: C:\Program Files\Apache Group\Tomcat 4.1\webapps\ROOT\marmon\properties\log4j.lcf (The system cannot find the file specified)
The log config file is --> \\properties\\log4j.lcf
Bootstrap: Service started
Thad Henry
Greenhorn

Joined: Sep 18, 2003
Posts: 5
Everyone,
Just wanted to let you all know that I found the problem. XP was adding a .TXT to each of my files and I didn't know it. When I moved the App over to 98 it showed in Explorer. I have now changed my XP to show all extensions. I would like to thank everyone for their time and understanding.
Thad
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having problems reading a file from servlet in Windows XP with Tomcat