You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Raimee <ra...@sympatico.ca> on 2000/09/27 06:51:20 UTC

Re: loading a properties file - where does it expect the file tobe?

Have you considered the System Properties Class?

//Declare a properties object.
protected Properties defaultProps;

...

     //Create and load default properties
        try {
          defaultProps = new Properties();
          FileInputStream in = new FileInputStream("myPropFile.properties");
          defaultProps.load(in);
          in.close();
        } catch (FileNotFoundException fnfe) {
          System.out.println("");
        } catch (IOException e) {
          System.out.println("");
        }

  //Get output stream to properties file for storing properties
  try {
      FileOutputStream out = new FileOutputStream("myPropFile.properties");
      defaultProps.put("txtFieldHomeDirectory", txtFieldHomeDirectory.getText());
      defaultProps.store(out, "---No Comment---");
      out.close();
    } catch (FileNotFoundException fnfe) {
      System.out.println("");
    } catch (IOException ioe) {
      System.out.println("");
    }

//Retrieve values from the properties object after it has been loaded and a property
has been previously saved.
String txtFieldHomeDirectory = defaultProps.getProperty("txtFieldHomeDirectory");


This is a sample value in the properties file:
txtFieldPropertiesFile=D\:\\JBuilder3\\myprojects\\TestDir\\recv

Hope this helps.

Raimee

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).
>
> Some info about my setup:
> 1.  Platform is Win2k Pro
> 2.  I use jk_nt_service to run Tomcat as a service
> 3.  I use mod_jk to connect through Apache Web Server 1.3.12
> 4.  I use a servlet mapping for the servlet that uses the non-servlet class
> 5.  To call the file, I use this syntax: FileInputStream propFile = new
> FileInputStream("my.properties");
>         The class assumes the properties file to be in the same directory that it
> exists in and, again, works from the command line as an application
>
> I've tried going through both port 80 (Apache) and 8080 (Tomcat) and I get
> the same behavior
>
> Any idea what is going on here?
>
> Jake


Re: loading a properties file - where does it expect the file tobe?

Posted by Daniel Barclay <Da...@digitalfocus.com>.
Raimee wrote:
> 
> Have you considered the System Properties Class?

Try ResourceBundle.



-- 
Daniel Barclay
Digital Focus
Daniel.Barclay@digitalfocus.com

Re: loading a properties file - where does it expect the file tobe?

Posted by Jacob Kjome <ja...@syntegra.com>.
I am doing something similar to this.  I do not use the FileOutputStream 
because I don't need to.  One difference is that I am using the 
java.util.Properties class rather than the System Properties class:

Here is my constructor for the class "DirectoryAction":

public DirectoryAction() {
         Properties props = new Properties();
         FileInputStream propFile = null;
         try {
                 // set up new properties object from file "ldap.properties"
                 propFile = new FileInputStream("my.properties");
                 props.load(propFile);
         } catch (FileNotFoundException e) {
                 System.err.println("Caught FileNotFoundException: " + 
e.getMessage());
         } catch (IOException e) {
                 System.err.println("Caught IOException: " + e.getMessage());
         } finally {
                 if (propFile != null) {
                         System.out.println("Closing FileInputStream...");
                         try {
                                 propFile.close();
                         } catch (IOException e) {
                                 System.err.println("Caught IOException: " 
+ e.getMessage());
                         }
                 } else {
                         System.out.println("FileInputStream not open");
                 }
         }

         //store properties in variables.... (one example below)
         LDAP_CONTEXT_FACTORY = props.getProperty("ldap.factory", 
"com.sun.jndi.ldap.LdapCtxFactory");
}


I also don't want to hardcode my path to the properties file.  I would 
think that I could keep it in the same directory as the class and not have 
to provide a path.

I just don't get why the above works in Tomcat 3.1 and not in 3.2b5????

BTW, I added some comments to a reply from another user on this 
thread.  Might want to read my repsponse on that one too.

thanks,

Jake


At 12:51 AM 9/27/2000 -0400, you wrote:
>Have you considered the System Properties Class?
>
>//Declare a properties object.
>protected Properties defaultProps;
>
>...
>
>      //Create and load default properties
>         try {
>           defaultProps = new Properties();
>           FileInputStream in = new FileInputStream("myPropFile.properties");
>           defaultProps.load(in);
>           in.close();
>         } catch (FileNotFoundException fnfe) {
>           System.out.println("");
>         } catch (IOException e) {
>           System.out.println("");
>         }
>
>   //Get output stream to properties file for storing properties
>   try {
>       FileOutputStream out = new FileOutputStream("myPropFile.properties");
>       defaultProps.put("txtFieldHomeDirectory", 
> txtFieldHomeDirectory.getText());
>       defaultProps.store(out, "---No Comment---");
>       out.close();
>     } catch (FileNotFoundException fnfe) {
>       System.out.println("");
>     } catch (IOException ioe) {
>       System.out.println("");
>     }
>
>//Retrieve values from the properties object after it has been loaded and 
>a property
>has been previously saved.
>String txtFieldHomeDirectory = 
>defaultProps.getProperty("txtFieldHomeDirectory");
>
>
>This is a sample value in the properties file:
>txtFieldPropertiesFile=D\:\\JBuilder3\\myprojects\\TestDir\\recv
>
>Hope this helps.
>
>Raimee
>
>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).
> >
> > Some info about my setup:
> > 1.  Platform is Win2k Pro
> > 2.  I use jk_nt_service to run Tomcat as a service
> > 3.  I use mod_jk to connect through Apache Web Server 1.3.12
> > 4.  I use a servlet mapping for the servlet that uses the non-servlet class
> > 5.  To call the file, I use this syntax: FileInputStream propFile = new
> > FileInputStream("my.properties");
> >         The class assumes the properties file to be in the same 
> directory that it
> > exists in and, again, works from the command line as an application
> >
> > I've tried going through both port 80 (Apache) and 8080 (Tomcat) and I get
> > the same behavior
> >
> > Any idea what is going on here?
> >
> > Jake