You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jacob Kjome <ja...@syntegra.com> on 2000/09/27 06:22:48 UTC

loading a properties file - where does it expect the file to be?

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 to be?

Posted by "Lempinen Sami (NET/Tampere)" <sa...@nokia.com>.
Greetings,

On Wed, Sep 27, 2000 at 08:04:23AM -0500, EXT Jacob Kjome wrote:
> The problem is, this isn't a servlet.  It is just a Class that I import 
> into the servlet and instantiate to communicate with an ldap directory 
> server.  I just had it in the same directory as the servlet for convenience 
> and this works on my 3.1 install and it always works when I run it as an 
> application from the command line.

To reiterate my previous message:

The concept of current working directory is undefined in a servlet
context. If the file was found when using 3.1, it was either a) sheer
luck or b) Tomcat had been started so that the working directory
magically matched your servlet context.

You have two ways around this (like I previously suggested):

- Somehow pass the servlet context path to your class so that you can
find the file relative to that, or,
- Use a ResourceBundle instead of Properties. The Bundle is found if
it's in the CLASSPATH.

-Sami


-- 
Sami Lempinen <sa...@nokia.com>
OSS Monitor / NMS Infoware Technologies
Blame Canada.

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

Posted by Jacob Kjome <ja...@syntegra.com>.
The problem is, this isn't a servlet.  It is just a Class that I import 
into the servlet and instantiate to communicate with an ldap directory 
server.  I just had it in the same directory as the servlet for convenience 
and this works on my 3.1 install and it always works when I run it as an 
application from the command line.

All problems henceforth will be assumed to be with 3.2b5

I also tried putting it in a directory somewhere in the system classpath, 
but I get the following error report from the servlet:

2000-09-26 11:31:17 - Ctx( /mycontext ): Exception in: R( /mycontext + 
/login + null) - java.lang.IllegalAccessError: try to access method 
DirectoryAction.authenticateUser(Ljava/lang/String;Ljava/lang/String;)Z 
from class Security
         at Security.doPost(Security.java:39)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at 
org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:387)
         at org.apache.tomcat.core.Handler.service(Handler.java:263)
         at 
org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:371)
         at 
org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:769)
         at 
org.apache.tomcat.core.ContextManager.service(ContextManager.java:715)
         at 
org.apache.tomcat.service.connector.Ajp12ConnectionHandler.processConnection(Ajp12ConnectionHandler.java:166)
         at 
org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:403)
         at 
org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:498)
         at java.lang.Thread.run(Thread.java:484)


I don't understand why this would happen.  It should be able to use this 
class just like any other.  I tired putting it in TOMCAT_HOME\classes and 
tried some other directory in my casspath.  I get the same error.

When I have the class + properties file in the same directory as my 
servlet, I don't get the error above, but I do get a FileNotFoundException 
which I catch and handle, but I just cannot load that file to grab the 
properties from it.

I would think that I was doing something wrong, but again, this works on my 
Tomcat 3.1 install just fine????


Jake

At 11:44 AM 9/27/2000 +0300, you wrote:
>On Tue, Sep 26, 2000 at 11:22:48PM -0500, EXT Jacob Kjome wrote:
>
> > 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???
>
>How are you specifying the path to the properties file? The concept of
>working directory is different from the servlet context path.
>
>Solution: either specify the path relative to the servlet context path
>as in
>
>    // Obtain the servlet context.
>    con = getServletContext();
>
>    String path = con.getRealPath("/")
>                  + System.getProperty("file.separator")
>                  + con.getInitParameter("propfile")
>
>or use a ResourceBundle which can be located anywhere in your
>classpath.
>
>-Sami
>--
>Sami Lempinen <sa...@nokia.com>
>OSS Monitor / NMS Infoware Technologies
>Blame Canada.


Re: Basic question: How do I get the equivolent of console messages for Tomcat?

Posted by Gerard Monsen <ge...@legalvista.com>.
         Thanks!  That was perfect!

                Gerard Monsen




Sandy McPherson wrote:

> They go to stdout and stderr which means that unless you redirect the output
> to a file they go to the tty from which tomcat was started.
>
> On Linux/UNIX
>
> tomcat.sh start > $TOMCAT_HOME/logs/system.out 2>
> $TOMCAT_HOME/logs/system.out
>
> or something along those lines. If you want to view it as it is written do
>
> tail -f $TOMCAT_HOME/logs/system.out
>
> Can't help you on Win32, sorry (unless of course you have cygwin32!)
>
> -----Original Message-----
> From: tomcat-user-return-12196-allsetbv=webmind.nl@jakarta.apache.org
> [mailto:tomcat-user-return-12196-allsetbv=webmind.nl@jakarta.apache.org]
> On Behalf Of Gerard Monsen
> Sent: Monday, October 02, 2000 7:48 AM
> To: tomcat-user@jakarta.apache.org
> Subject: Basic question: How do I get the equivolent of console messages
> for Tomcat?
>
>         Hi!  Basic question:  I just compiled a rather complicated
> servlet and tried to test it out using Tomcat.  Unfortunately, it
> doesn't load up, which I'm not suprised about.  How do I get
> the equivolent of console messages for Tomcat.  In other words,
> where do Exception reports get dumped to and where can I
> see the results of System.out.println() debugging messages that
> I might place in my program?
>        I tried the "servlet.log" file, but all it says is:
>
> Context log path ="/TestServlet" :tomcat.errorPage: init
>
>       I assume this means that servlet bombed out on loading
> up, but how do I get the exception information so that I
> can fix whatever caused the servlet not to load?
>
> Gerard Monsen
> Oakland, CA


RE: Basic question: How do I get the equivolent of console messages for Tomcat?

Posted by Sandy McPherson <al...@webmind.nl>.
They go to stdout and stderr which means that unless you redirect the output
to a file they go to the tty from which tomcat was started.

On Linux/UNIX

tomcat.sh start > $TOMCAT_HOME/logs/system.out 2>
$TOMCAT_HOME/logs/system.out

or something along those lines. If you want to view it as it is written do

tail -f $TOMCAT_HOME/logs/system.out

Can't help you on Win32, sorry (unless of course you have cygwin32!)

-----Original Message-----
From: tomcat-user-return-12196-allsetbv=webmind.nl@jakarta.apache.org
[mailto:tomcat-user-return-12196-allsetbv=webmind.nl@jakarta.apache.org]
On Behalf Of Gerard Monsen
Sent: Monday, October 02, 2000 7:48 AM
To: tomcat-user@jakarta.apache.org
Subject: Basic question: How do I get the equivolent of console messages
for Tomcat?


        Hi!  Basic question:  I just compiled a rather complicated
servlet and tried to test it out using Tomcat.  Unfortunately, it
doesn't load up, which I'm not suprised about.  How do I get
the equivolent of console messages for Tomcat.  In other words,
where do Exception reports get dumped to and where can I
see the results of System.out.println() debugging messages that
I might place in my program?
       I tried the "servlet.log" file, but all it says is:

Context log path ="/TestServlet" :tomcat.errorPage: init

      I assume this means that servlet bombed out on loading
up, but how do I get the exception information so that I
can fix whatever caused the servlet not to load?

Gerard Monsen
Oakland, CA



Basic question: How do I get the equivolent of console messages for Tomcat?

Posted by Gerard Monsen <ge...@legalvista.com>.
        Hi!  Basic question:  I just compiled a rather complicated
servlet and tried to test it out using Tomcat.  Unfortunately, it
doesn't load up, which I'm not suprised about.  How do I get
the equivolent of console messages for Tomcat.  In other words,
where do Exception reports get dumped to and where can I
see the results of System.out.println() debugging messages that
I might place in my program?
       I tried the "servlet.log" file, but all it says is:

Context log path ="/TestServlet" :tomcat.errorPage: init

      I assume this means that servlet bombed out on loading
up, but how do I get the exception information so that I
can fix whatever caused the servlet not to load?

Gerard Monsen
Oakland, CA


Using EmbededTomcat

Posted by Derek DeMoro <DE...@oracle.com>.
Hi Craig,
    I'm having a problem using the EmbededTomcat.java file.  Everytime
I run it, I get the following errors.
ContextManager: Adding context Ctx(  )
cannot process configuration web service using " /e:/tomcat/webapps/ROOT"
Security init for Context /e:/tomcat/webapps/ROOT failed
java.security.AccessControlException: access denied (java.lang.RuntimePermission
createClassLoader )
 void java.security.AccessControlContext.checkPermission(java.security.Permission)
 void java.security.AccessController.checkPermission(java.security.Permission)
 void java.lang.SecurityManager.checkPermission(java.security.Permission)
 void java.lang.SecurityManager.checkCreateClassLoader()
 void java.lang.ClassLoader.<init>()
 void org.apache.tomcat.loader.AdaptiveClassLoader.<init>()
 void org.apache.tomcat.loader.AdaptiveServletLoader.<init>()
 void
org.apache.tomcat.context.LoaderInterceptor.contextInit(org.apache.tomcat.core.Context)
 void org.apache.tomcat.core.ContextManager.initContext(org.apache.tomcat.core.Context)
 void org.apache.tomcat.startup.EmbededTomcat.initContext(javax.servlet.ServletContext)
 void org.apache.tomcat.startup.EmbededTomcat.main(java.lang.String[])
PoolTcpConnector: Starting HttpConnectionHandler on 8080






Could you please help.  I need to get
this working inside of Oracle Jdeveloper 5.0, for Oracle OpenWorld next
week.

Thanks,
Derek DeMoro
Oracle Corp.


"Craig R. McClanahan" wrote:

> 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 <http://www.apachecon.com>!
> Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
> Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
>                                     Applications to Tomcat


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

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
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 <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat



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


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

Posted by "Lempinen Sami (NET/Tampere)" <sa...@nokia.com>.
On Tue, Sep 26, 2000 at 11:22:48PM -0500, EXT Jacob Kjome wrote:

> 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???

How are you specifying the path to the properties file? The concept of
working directory is different from the servlet context path.

Solution: either specify the path relative to the servlet context path
as in 

   // Obtain the servlet context.
   con = getServletContext();

   String path = con.getRealPath("/")
                 + System.getProperty("file.separator")
                 + con.getInitParameter("propfile")

or use a ResourceBundle which can be located anywhere in your
classpath.

-Sami
-- 
Sami Lempinen <sa...@nokia.com>
OSS Monitor / NMS Infoware Technologies
Blame Canada.

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 Raimee <ra...@sympatico.ca>.
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