Hibernate and log4j config under Tomcat.

Discussion in 'Java' started by Rico, Feb 18, 2005.

  1. Rico

    Rico Guest

    "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
    WEB-INF/classes directory of your web-applications. Log4j will find the
    properties file and initialize itself. This is easy to do and it works."

    "The XML configuration file is placed in the context classpath
    (WEB-INF/classes), as hibernate.cfg.xml"

    I don't know what's Tomcat's problem but in both cases, despite what the
    documentation says, placing the config files in WEB-INF/classes didn't
    work.
    For log4j I ended up using an initialization servlet.
    For Hibernate, I placed the xml file in several folders after some
    googling:

    WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
    <tomcat_home>/conf

    Nothing worked and this time Tomcat explicitly told me that the file is
    not to be found. I ended up having to specify an absolute path to at least
    see that the code I'm actually interested in works.

    I don't know what's Tomcat's problem... or mine...

    Rico.
     
    Rico, Feb 18, 2005
    #1
    1. Advertising

  2. Rico

    Ryan Stewart Guest

    "Rico" <> wrote in message
    news:p...
    > "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
    > WEB-INF/classes directory of your web-applications. Log4j will find the
    > properties file and initialize itself. This is easy to do and it works."
    >
    > "The XML configuration file is placed in the context classpath
    > (WEB-INF/classes), as hibernate.cfg.xml"
    >
    > I don't know what's Tomcat's problem but in both cases, despite what the
    > documentation says, placing the config files in WEB-INF/classes didn't
    > work.
    > For log4j I ended up using an initialization servlet.
    > For Hibernate, I placed the xml file in several folders after some
    > googling:
    >
    > WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
    > <tomcat_home>/conf
    >
    > Nothing worked and this time Tomcat explicitly told me that the file is
    > not to be found. I ended up having to specify an absolute path to at least
    > see that the code I'm actually interested in works.
    >
    > I don't know what's Tomcat's problem... or mine...
    >
    > Rico.

    Likely yours. I just set up a fresh Hibernate install on Tomcat 5.5 (with Log4j)
    with no problems. Define what you mean by "didn't work".
     
    Ryan Stewart, Feb 18, 2005
    #2
    1. Advertising

  3. Rico

    Rico Guest

    On Thu, 17 Feb 2005 23:31:04 -0600, Ryan Stewart wrote:
    > "Rico" <> wrote in message


    >> "The XML configuration file is placed in the context classpath
    >> (WEB-INF/classes), as hibernate.cfg.xml"


    >> For Hibernate, I placed the xml file in several folders after some
    >> googling:
    >>
    >> WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
    >> <tomcat_home>/conf
    >>
    >> Nothing worked and this time Tomcat explicitly told me that the file is
    >> not to be found. I ended up having to specify an absolute path to at
    >> least see that the code I'm actually interested in works.
    >>
    >> I don't know what's Tomcat's problem... or mine...
    >>
    >> Rico.

    > Likely yours. I just set up a fresh Hibernate install on Tomcat 5.5
    > (with Log4j) with no problems. Define what you mean by "didn't work".


    As I said, for Hibernate under Tomcat (4.1.18 and 4.1.31), no matter where
    I put the hibernate.cfg.xml, including WEB-INF/classes, I got the message
    that the file is not to be found when we reach the following code:

    sessionFactory = new Configuration().configure()
    .buildSessionFactory();

    2005-02-18 15:28:00,968 WARN http-8080-Processor4
    net.sf.hibernate.cfg.Configuration - /hibernate.cfg.xml not found

    I've considered trying Tomcat 5.0.x but it seems that first I need to
    figure out which ones and adapt a couple of settings in server.xml and/or
    web.xml to get it to stop crashing.

    Rico.
     
    Rico, Feb 18, 2005
    #3
  4. Rico

    Ryan Stewart Guest

    "Rico" <> wrote in message
    news:p...
    > As I said, for Hibernate under Tomcat (4.1.18 and 4.1.31), no matter where
    > I put the hibernate.cfg.xml, including WEB-INF/classes, I got the message
    > that the file is not to be found when we reach the following code:
    >
    > sessionFactory = new Configuration().configure()
    > .buildSessionFactory();
    >
    > 2005-02-18 15:28:00,968 WARN http-8080-Processor4
    > net.sf.hibernate.cfg.Configuration - /hibernate.cfg.xml not found
    >
    > I've considered trying Tomcat 5.0.x but it seems that first I need to
    > figure out which ones and adapt a couple of settings in server.xml and/or
    > web.xml to get it to stop crashing.
    >

    What version of Hibernate are you using? When I remove the config file, I get
    this in the log: (line breaks inserted to prevent wrap and logging set to INFO
    level)
    INFO [http-8080-Processor25] cfg.Environment (<clinit>:483)
    - Hibernate 2.1.8
    INFO [http-8080-Processor25] cfg.Environment (<clinit>:512)
    - hibernate.properties not found
    INFO [http-8080-Processor25] cfg.Environment (<clinit>:543)
    - using CGLIB reflection optimizer
    INFO [http-8080-Processor25] cfg.Environment (<clinit>:572)
    - using JDK 1.4 java.sql.Timestamp handling
    INFO [http-8080-Processor25] cfg.Configuration (configure:909)
    - configuring from resource: /hibernate.cfg.xml
    INFO [http-8080-Processor25] cfg.Configuration
    (getConfigurationInputStream:881)
    - Configuration resource: /hibernate.cfg.xml
    WARN [http-8080-Processor25] cfg.Configuration
    (getConfigurationInputStream:885)
    - /hibernate.cfg.xml not found
    ERROR [http-8080-Processor25] persistence.HibernateUtil (<clinit>:19)
    - Initial SessionFactory creation failed.
    net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found

    Does that look like what you're getting? I assume you're using some build tool.
    Are you sure that the config file is being built to the right place?
     
    Ryan Stewart, Feb 18, 2005
    #4
  5. Rico

    Rico Guest

    On Fri, 18 Feb 2005 06:53:49 -0600, Ryan Stewart wrote:
    > What version of Hibernate are you using? When I remove the config file,
    > I get this in the log: (line breaks inserted to prevent wrap and logging
    > set to INFO level)
    > INFO [http-8080-Processor25] cfg.Environment (<clinit>:483)
    > - Hibernate 2.1.8
    > INFO [http-8080-Processor25] cfg.Environment (<clinit>:512)
    > - hibernate.properties not found
    > INFO [http-8080-Processor25] cfg.Environment (<clinit>:543)
    > - using CGLIB reflection optimizer
    > INFO [http-8080-Processor25] cfg.Environment (<clinit>:572)
    > - using JDK 1.4 java.sql.Timestamp handling
    > INFO [http-8080-Processor25] cfg.Configuration (configure:909)
    > - configuring from resource: /hibernate.cfg.xml
    > INFO [http-8080-Processor25] cfg.Configuration
    > (getConfigurationInputStream:881)
    > - Configuration resource: /hibernate.cfg.xml
    > WARN [http-8080-Processor25] cfg.Configuration
    > (getConfigurationInputStream:885)
    > - /hibernate.cfg.xml not found
    > ERROR [http-8080-Processor25] persistence.HibernateUtil (<clinit>:19)
    > - Initial SessionFactory creation failed.
    > net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
    >
    > Does that look like what you're getting? I assume you're using some
    > build tool. Are you sure that the config file is being built to the
    > right place?


    I was using 2.1.6 and I upgraded to 2.1.8. The above is almost exactly
    what I still get. I am positive that I have a file hibernate.cfg.xml in my
    application's WEB-INF/classes folder.
    It's the same config file that was at the root of the Eclipse project that
    didn't require an absolute path to find it.

    Rico.
     
    Rico, Feb 18, 2005
    #5
  6. Rico

    Rico Guest

    On Fri, 18 Feb 2005 10:04:17 +0800, Rico wrote:
    > "Under Tomcat 3.x and 4.x, you should place the log4j.properties under the
    > WEB-INF/classes directory of your web-applications. Log4j will find the
    > properties file and initialize itself. This is easy to do and it works."
    >
    > "The XML configuration file is placed in the context classpath
    > (WEB-INF/classes), as hibernate.cfg.xml"
    >
    > I don't know what's Tomcat's problem but in both cases, despite what the
    > documentation says, placing the config files in WEB-INF/classes didn't
    > work.
    > For log4j I ended up using an initialization servlet.
    > For Hibernate, I placed the xml file in several folders after some
    > googling:
    >
    > WEB-INF/classes ; WEB-INF/classes/<path to class file> ; WEB-INF/src ;
    > <tomcat_home>/conf
    >
    > Nothing worked and this time Tomcat explicitly told me that the file is
    > not to be found. I ended up having to specify an absolute path to at least
    > see that the code I'm actually interested in works.
    >
    > I don't know what's Tomcat's problem... or mine...
    >
    > Rico.


    Believe me, I've really had to keep trying before getting to this point:

    Tomcat can now see hibernate.cfg.xml and log4j.properties which are in my
    WEB-INF/classes folder as per the documentation.
    What isn't in the documentation is whether the absolute path for the web
    application needs to be
    <CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes

    because from what I observe, only then things work as they are supposed to.
    If I am to use another installation path for the application, I'm stuck
    with e.g using initialization servlet or probably a ResourceBundle to pass
    the path to Hibernate's Configuration class ?

    Rico.
     
    Rico, Feb 19, 2005
    #6
  7. Rico

    Ryan Stewart Guest

    "Rico" <> wrote in message
    news:p...
    > Believe me, I've really had to keep trying before getting to this point:
    >
    > Tomcat can now see hibernate.cfg.xml and log4j.properties which are in my
    > WEB-INF/classes folder as per the documentation.
    > What isn't in the documentation is whether the absolute path for the web
    > application needs to be
    > <CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes
    >

    No, it does not.

    > because from what I observe, only then things work as they are supposed to.
    > If I am to use another installation path for the application, I'm stuck
    > with e.g using initialization servlet or probably a ResourceBundle to pass
    > the path to Hibernate's Configuration class ?
    >

    What do you mean by "another installation path"? What path, and how do you set
    it up for Tomcat to access it?
     
    Ryan Stewart, Feb 20, 2005
    #7
  8. Rico

    Rico Guest

    On Sat, 19 Feb 2005 23:27:05 -0600, Ryan Stewart wrote:
    > "Rico" <> wrote in message
    >> Believe me, I've really had to keep trying before getting to this point:
    >>
    >> Tomcat can now see hibernate.cfg.xml and log4j.properties which are in
    >> my WEB-INF/classes folder as per the documentation. What isn't in the
    >> documentation is whether the absolute path for the web application
    >> needs to be
    >> <CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes
    >>

    > No, it does not.
    >
    >> because from what I observe, only then things work as they are supposed
    >> to. If I am to use another installation path for the application, I'm
    >> stuck with e.g using initialization servlet or probably a
    >> ResourceBundle to pass the path to Hibernate's Configuration class ?
    >>

    > What do you mean by "another installation path"? What path, and how do
    > you set it up for Tomcat to access it?


    C:\\myWebApp

    <Context path="/something" docBase="C:\\myWebApp" />

    In such a case, Tomcat cannot find
    C:\myWebApp\WEB-INF\classes\hibernate.cfg.xml

    Same goes for
    C:\myWebApp\WEB-INF\classes\log4j.properties

    Though from the way I understand the documentation they're supposed to be
    found.
    It's when I use the following installation folder that e.g
    Hibernate's no-args configure() succeeds as Tomcat can find the
    hibernate.cfg.xml in:

    <CATALINA_HOME>/webapps/<app_folder>/WEB_INF/classes

    Rico.
     
    Rico, Feb 20, 2005
    #8
  9. Rico

    Ryan Stewart Guest

    "Rico" <> wrote in message
    news:p...
    > <Context path="/something" docBase="C:\\myWebApp" />

    Try docBase="C:\myWebApp".
     
    Ryan Stewart, Feb 20, 2005
    #9
  10. Rico

    Rico Guest

    On Sun, 20 Feb 2005 08:34:47 -0600, Ryan Stewart wrote:
    > "Rico" <> wrote in message


    >> <Context path="/something" docBase="C:\\myWebApp" />

    > Try docBase="C:\myWebApp".


    humm... any particular reason why we'd expect that to make a difference?
     
    Rico, Feb 20, 2005
    #10
  11. Rico

    Ryan Stewart Guest

    "Rico" <> wrote in message
    news:p...
    > On Sun, 20 Feb 2005 08:34:47 -0600, Ryan Stewart wrote:
    >> "Rico" <> wrote in message

    >
    >>> <Context path="/something" docBase="C:\\myWebApp" />

    >> Try docBase="C:\myWebApp".

    >
    > humm... any particular reason why we'd expect that to make a difference?


    Not particularly. It's just an inconsistency that I noticed. This is an OS path,
    not a Java String. You don't need to escape the '\'s. It *should* work the same
    regardless, but you never know. As for your problem in general, either you've
    got a corrupted installation or you're not telling some vital bit of
    information, because the behavior you've described is not normal. Here is a
    complete context that I have saved as
    "$CATALINA_BASE\conf\Catalina\localhost\recipes.xml" on WinXP Home:
    <Context docBase="C:\Projects\recipes\target\recipes"
    reloadable="true"
    backgroundProcessorDelay="2">
    <Resource name="jdbc/recipes" auth="Container" type="javax.sql.DataSource"
    maxActive="10" maxIdle="2" maxWait="10000"
    username="root" password="root"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/reciperack"/>
    </Context>

    Obviously you can ignore the Resource element. The corresponding exploded web
    application is at "C:\Projects\recipes\target\recipes", just as in the docBase
    attribute. In "C:\Projects\recipes\target\recipes\WEB-INF\classes", I have
    hibernate.cfg.xml and log4j.properties, not to mention a Struts resource file,
    jndi.properties, and hibernate mapping file, all of which are found on the
    classpath when needed.
     
    Ryan Stewart, Feb 21, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandra Stehman

    eclipse, junit, log4j, & finding log4j.xml

    Alexandra Stehman, Jan 19, 2004, in forum: Java
    Replies:
    0
    Views:
    5,806
    Alexandra Stehman
    Jan 19, 2004
  2. Rodolfo
    Replies:
    0
    Views:
    968
    Rodolfo
    Jul 12, 2004
  3. Rob Mitchell
    Replies:
    0
    Views:
    2,204
    Rob Mitchell
    Jan 25, 2006
  4. news.rcn.com
    Replies:
    0
    Views:
    711
    news.rcn.com
    Apr 24, 2007
  5. Replies:
    0
    Views:
    383
Loading...

Share This Page