Return-Path: Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 90293 invoked from network); 27 Sep 2000 18:02:44 -0000 Received: from ns1.syntegra.com (150.143.16.2) by locus.apache.org with SMTP; 27 Sep 2000 18:02:44 -0000 Received: from [129.179.161.11] by ns1.cdc.com with ESMTP for tomcat-user@jakarta.apache.org; Wed, 27 Sep 2000 13:02:14 -0500 Received: from ns1.cdc.com by cdsms.cdc.com with ESMTP for tomcat-user@jakarta.apache.org; Wed, 27 Sep 2000 13:02:13 -0500 Received: from [206.9.170.61] by ns1.cdc.com with ESMTP for tomcat-user@jakarta.apache.org; Wed, 27 Sep 2000 13:02:13 -0500 Received: by cis-exchange.internal.cis with Internet Mail Service (5.5.2650.21) id ; Wed, 27 Sep 2000 13:01:17 -0500 Message-Id: <51E54DD2477FCB4FB4E21616A5B2FA2C021F89@cis-exchange.internal.cis> From: Jacob Kjome To: "'tomcat-user@jakarta.apache.org'" Subject: RE: loading a properties file - where does it expect the file tobe? Date: Wed, 27 Sep 2000 13:01:15 -0500 X-Mailer: Internet Mail Service (5.5.2650.21) MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Spam-Rating: locus.apache.org 1.6.2 0/1000/N Thank you! That is exactly what I was looking for :-) Jake -----Original Message----- From: Craig R. McClanahan [mailto:Craig.McClanahan@eng.sun.com] Sent: Wednesday, September 27, 2000 12:38 PM To: tomcat-user@jakarta.apache.org Subject: Re: loading a properties file - where does it expect the file tobe? Jacob Kjome wrote: > The following isn't a problem for me in Tomcat 3.1, but is in 3.2b5 > > I have a servlet that uses another class that isn't a servlet which loads a > file with properties in it. > > When I run the class as an application, it works fine. > > However, when calling it from a servlet, it can't find the file??? > > The location of my non-servlet class is, currently, in the same directory > as my servlet class (and so is my properties file). > The problem you are having is based on the fact that you are trying to use a filename, which is resolved by the OS as relative to the current working directory. When you are running in a servlet container, the current working directory is not in your control. The best way to deal with stuff like this is to not use files at all. Instead, use Class.getResourceAsStream() as the mechanism to load your properties from the same place that the class file is. Example code (assuming the properties filename is "myprops.properties": Properties props = new Properties(); try { InputStream stream = this.getClass().getResourceAsStream("myprops.properties"); props.load(stream); stream.close(); } catch (Throwable t) { ... deal with exception ... } The reason this is better is that it works even when the class (and the corresponding properties file) are packaged into a JAR file on the classpath (or installed in WEB-INF/lib in a web application). In this scenario, file I/O is not going to work at all, because the properties are not stored in a separate file at all. The getResourceAsStream() method deals with all of that for you. > > Jake Craig McClanahan ==================== See you at ApacheCon Europe ! Session VS01 (23-Oct 13h00-17h00): Sun Technical Briefing Session T06 (24-Oct 14h00-15h00): Migrating Apache JServ Applications to Tomcat