relative paths with Tomcat
Greetings,
I have, in a Tomcat project, a regular Java class (not a servlet) which reads data from a text file. The java files have been placed in the WEB-INF folder. Where should I place the text file and how can I access it from within the Java class using a relative path? It works fine with an absolute path, but I really need to use a relative one. I've seen some code snippets with the ServletContext interface, but my class isn't a servlet, so I'm not sure if its applicable.
Or perhaps my java class file should be in some other location (other than WEB-INF) seeing as its not a servlet?
Please help!
Thanks
> Greetings,
>
> I have, in a Tomcat project, a regular Java class
> (not a servlet) which reads data from a text file.
> The java files have been placed in the WEB-INF
> folder.
You mean that the .class file is in the WEB-INF/classes directory, right?
> Where should I place the text file and how
> can I access it from within the Java class using a
> relative path? It works fine with an absolute path,
> but I really need to use a relative one. I've seen
> some code snippets with the ServletContext interface,
> but my class isn't a servlet, so I'm not sure if its
> applicable.
You class won't have access to the servlet context, but it should be able to get whatever it needs from the servlet.
> Or perhaps my java class file should be in some other
> location (other than WEB-INF) seeing as its not a
> servlet?
>
> Please help!
No, your app is a Web app, and all classes in that context share a CLASSPATH. Any .txt or properties file that you need can be found relative to WEB-INF/classes if you use the getResourceAsStream() method in the servlet context to read it. That way you don't have absolute path problems to worry about, because it's all relative to your Web app's context.
%