RE: How to find a file from a class with Tomcat

2001-12-19 Thread Bongiorno.Christian

Stick the resource in along with the JAR. Then they will be loaded together and a 
simple call to the file without
a path should discover it



-Original Message-
From: Stephan Wiesner [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 19, 2001 9:36 AM
To: Tomcat Users List
Subject: How to find a file from a class with Tomcat


I have a Servlet S.java which uses a class C.java (not a servlet itself,
could make it one if I had too).
C.java needs to read a config file. The problem is that this class thinks it
is in c:/ or wherever I started the tomcat server from. I can use
this.getClass().getResource(fileName); but that only works with
Properties.load(), not with a FileInputStream.
So at the moment I have all my paths hardcoded into the classes and of
course I don't want that.

My server.xml contains:



And the local web.xml:
   
  hi
  HelloWorldExample
   

  hi
  /hi/*
   


Any tipp would be appreciated.

Stephan Wiesner


--
To unsubscribe:   
For additional commands: 
Troubles with the list: 


--
To unsubscribe:   
For additional commands: 
Troubles with the list: 




RE: How to find a file from a class with Tomcat

2001-12-19 Thread Barney Hamish

Why don't you put it in the web.xml
  
directory
[my directory]
  

Then you can grab it from your servlet with a get Context Parameter call.


-Original Message-
From: Stephan Wiesner [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 19, 2001 3:36 PM
To: Tomcat Users List
Subject: How to find a file from a class with Tomcat


I have a Servlet S.java which uses a class C.java (not a servlet itself,
could make it one if I had too).
C.java needs to read a config file. The problem is that this class thinks it
is in c:/ or wherever I started the tomcat server from. I can use
this.getClass().getResource(fileName); but that only works with
Properties.load(), not with a FileInputStream.
So at the moment I have all my paths hardcoded into the classes and of
course I don't want that.

My server.xml contains:



And the local web.xml:
   
  hi
  HelloWorldExample
   

  hi
  /hi/*
   


Any tipp would be appreciated.

Stephan Wiesner


--
To unsubscribe:   
For additional commands: 
Troubles with the list: 

--
To unsubscribe:   
For additional commands: 
Troubles with the list: 




RE: How to find a file from a class with Tomcat

2001-12-19 Thread Randy Layman


If you only need a stream (and not FileInputStream) you can use
getResouceAsStream, which will return an InputStream, from which you can
create just about any other stream that you want.

Randy


> -Original Message-
> From: Stephan Wiesner [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, December 19, 2001 9:36 AM
> To: Tomcat Users List
> Subject: How to find a file from a class with Tomcat
> 
> 
> I have a Servlet S.java which uses a class C.java (not a 
> servlet itself,
> could make it one if I had too).
> C.java needs to read a config file. The problem is that this 
> class thinks it
> is in c:/ or wherever I started the tomcat server from. I can use
> this.getClass().getResource(fileName); but that only works with
> Properties.load(), not with a FileInputStream.
> So at the moment I have all my paths hardcoded into the classes and of
> course I don't want that.
> 
> My server.xml contains:
>   
> docBase="d:/jdk1.3/jakarta-tomcat-3.2.3/webapps/examples"
>  crossContext="false"
>  debug="9"
>  reloadable="true" >
> 
> 
> And the local web.xml:
>
>   hi
>   HelloWorldExample
>
> 
>   hi
>   /hi/*
>
> 
> 
> Any tipp would be appreciated.
> 
> Stephan Wiesner
> 
> 
> --
> To unsubscribe:   
> For additional commands: 
> Troubles with the list: 
> 

--
To unsubscribe:   
For additional commands: 
Troubles with the list: 




Re: How to find a file from a class with Tomcat

2001-12-19 Thread Stephan Wiesner

Thanks for all the quick tipps!
Okay, here is what works for me:
InputStream in = this.getClass().getResourceAsStream("1.txt");
int c = 0;
out.println("");
while(( c = in.read()) != -1)
   out.print(((char)c) + "");
out.println("");


Stephan

- Original Message -
From: "Ralph Einfeldt" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Sent: Wednesday, December 19, 2001 3:43 PM
Subject: AW: How to find a file from a class with Tomcat


> You have several Options.
> Just one: getResourceAsStream().
>
> I think others will provide other solutions.
> which is the best, depends on your needs.
>
> > -Ursprüngliche Nachricht-
> > Von: Stephan Wiesner [mailto:[EMAIL PROTECTED]]
> > Gesendet: Mittwoch, 19. Dezember 2001 15:36
> > An: Tomcat Users List
> > Betreff: How to find a file from a class with Tomcat
> >
> >
> > I have a Servlet S.java which uses a class C.java (not a
> > servlet itself,
> > could make it one if I had too).
> > C.java needs to read a config file. The problem is that this
> > class thinks it
> > is in c:/ or wherever I started the tomcat server from. I can use
> > this.getClass().getResource(fileName); but that only works with
> > Properties.load(), not with a FileInputStream.
> > So at the moment I have all my paths hardcoded into the classes and of
> > course I don't want that.
> >
> > My server.xml contains:
> >  >
> > docBase="d:/jdk1.3/jakarta-tomcat-3.2.3/webapps/examples"
> >  crossContext="false"
> >  debug="9"
> >  reloadable="true" >
> > 
> >
> > And the local web.xml:
> >
> >   hi
> >   HelloWorldExample
> >
> > 
> >   hi
> >   /hi/*
> >
> >
> >
> > Any tipp would be appreciated.
> >
> > Stephan Wiesner
> >
> >
> > --
> > To unsubscribe:   
> > For additional commands: 
> > Troubles with the list: 
> >
> >
> >
>
> --
> To unsubscribe:   
> For additional commands: 
> Troubles with the list: 


--
To unsubscribe:   
For additional commands: 
Troubles with the list: 




Re: How to find a file from a class with Tomcat

2001-12-19 Thread Bo Xu

- Original Message -
From: "Stephan Wiesner" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Sent: Wednesday, December 19, 2001 9:36 AM
Subject: How to find a file from a class with Tomcat


> I have a Servlet S.java which uses a class C.java (not a servlet itself,
> could make it one if I had too).
> C.java needs to read a config file. The problem is that this class thinks
it
> is in c:/ or wherever I started the tomcat server from. I can use
> this.getClass().getResource(fileName); but that only works with
> Properties.load(), not with a FileInputStream.
> So at the moment I have all my paths hardcoded into the classes and of
> course I don't want that.
>[...]



* java.io.InputStream and/or java.util.Properties
- InputStream is =
   this.getClass().getClassLoader().getResourceAsStream("xyz.properties");
 - InputStream is =
   this.getClass().getResourceAsStream("testApp.properties");
- InputStream is =

Thread.currentThread().getContextClassLoader().getResourceAsStream("myservle
t.conf");
- InputStream is =
   getServletContext().getResourceAsStream("/WEB-INF/xyz.properties");

-  Properties props = new Properties();
props.load(is);
is.close();

* use java.util.ResourceBundle

* use file-system directly


//haha :-)
Bo
Dec.19, 2001



--
To unsubscribe:   
For additional commands: 
Troubles with the list: