Using relative path in java programs - how ?  
Author Message
ni4ni





PostPosted: 2004-6-15 15:59:00 Top

java-programmer, Using relative path in java programs - how ? Hi

I wrote a new servlet that uses a configuration file to store
parameters.
The problem is that I want to use a relative path when accessing this
file, since I dont know where this servlet will be deployed.
Basically, I want it to be in the same directory where the .class
files are located.

Using the property user.dir does'nt help because it returns the
server's executable path.

How can I control this ? how can the program "know" where the class
files are ?
 
Dan Gravell





PostPosted: 2004-6-15 16:20:00 Top

java-programmer >> Using relative path in java programs - how ? Guy wrote:

> Hi
>
> I wrote a new servlet that uses a configuration file to store
> parameters.
> The problem is that I want to use a relative path when accessing this
> file, since I dont know where this servlet will be deployed.
> Basically, I want it to be in the same directory where the .class
> files are located.
>
> Using the property user.dir does'nt help because it returns the
> server's executable path.
>
> How can I control this ? how can the program "know" where the class
> files are ?

Look at the ServletContext object, which you should be able to get from
your Servlet. You normally place the config file somewhere like WEB-INF
and then do a getResource() or getResourceAsStream() (sorry I can't
remember specifics, it's been a while).

Dan
 
Andrew Thompson





PostPosted: 2004-6-15 16:37:00 Top

java-programmer >> Using relative path in java programs - how ? On 15 Jun 2004 00:59:00 -0700, Guy wrote:
..
> I wrote a new servlet that uses a configuration file to store
> parameters.
> The problem is that I want to use a relative path when accessing this
> file, since I dont know where this servlet will be deployed.

Put the config. file in a known place on the
server (the servlet is another matter), then..
URL url = getServletContext().getResource(fileName);

But then, I understand the accepted way to
configure servlets is by adding the information
to the deployment descriptor and invoking...

String param = ServletContext.getInitParameter(name);

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology