Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 18th, 2003, 05:20 PM
HowardHyde HowardHyde is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 HowardHyde User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question File I/O from servlet

My servlet calls another java class that reads configuration information from an external xml file and returns it to the servlet. This other class resides in a subdirectory of my web application. I do not want to include this other xml file's data in the web.xml,
AND I DON'T WANT TO HARD-CODE THE PATH to the xml file, but unless I do, my app can't find it.

My class contains the line
protected static final String configXMLFileName = "myConfig.xml";
...
later code opens the file and parses it.

If I put the complete path in this variable, it works. Where can I drop this file into a 'default' directory where the app will be able to read it, without any path info in the filename? I've tried web-inf, the subdirectory where my class lives, 'C:\Program Files\Apache Group\Tomcat 4.1\bin', all to no avail. No matter what, I get:

java.io.FileNotFoundException: myConfig.xml (The system cannot find the file specified)

Thanks in advance for your help.

Reply With Quote
  #2  
Old March 19th, 2003, 09:32 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
If the class you are using resides in a folder located in the WEB-INF/bin folder, then you can put the xml file in the root of the bin folder and access it using the "/myConfig.xml" path. If you want, you can put the xml file in the folder the actual calss resides in and then you can use the file name without any path information.

For example, if the class is in a package called com.mystuff.myclass, then you could put the xml file in WEB-INF/bin/com/mystuff and access it simply by using the myConfig.xml file name in the FileInputStream or whatever.

Reply With Quote
  #3  
Old March 19th, 2003, 10:55 AM
HowardHyde HowardHyde is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 HowardHyde User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for your help.
All of my servlet application classes reside under WEB-INF/classes. I have tried putting the xml file in the same folder as the class, and refering to it in code as "/myConfig.xml" and "myConfig.xml" (with and without the slash), all to no avail.

The only thing I've found to work is to drop the file into the launch directory of Tomcat, "C:\Documents and Settings\Administrator\Start Menu\Programs\Apache Tomcat 4.1". But that makes the file global to the webserver, not what I want. I want the file to be global only to a servlet context / web application.

What have I missed? Is there some special significance to WEB-INF/bin? I haven't heard of that before. Where can I learn more?

Reply With Quote
  #4  
Old March 19th, 2003, 11:06 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
Sorry, I meant "classes", not "bin". I was having a mental lapse.

I don't know why you can't find the file if you have put it in the same folder as the class file and referred to it by name. This works for me in my application, but I am not using Tomcat. I have Websphere 5.0.

Last edited by Nemi : March 19th, 2003 at 11:09 AM.

Reply With Quote
  #5  
Old March 19th, 2003, 11:14 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
I take that back. I use getClass().getResourceAsStream("/filename.xml"), and not just a FileInputStream. This is likely your problem. Using the classes Class.getResourceAsStream looks for the file relative to the location of the class file you are using. Otherwise you would have to supply a system path.

Last edited by Nemi : March 19th, 2003 at 11:25 AM.

Reply With Quote
  #6  
Old March 19th, 2003, 12:19 PM
HowardHyde HowardHyde is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 6 HowardHyde User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Whew! I got it! It works, without the leading slash, using the following line:
InputStream fis = new myConfig().getClass().getResourceAsStream ( "xmlFileName" );

The file can now reside in the same directory with the class that reads it.

Thanks for going the extra mile for me.

Reply With Quote
  #7  
Old March 19th, 2003, 12:25 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
No problem.

You can save the creation of that object by using (I am assuming your class name is myConfig)
Code:
myConfig.class.getResourceAsStream ( "xmlFileName" );

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > File I/O from servlet


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway