-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: read hibernate.cfg.xml from within a jar file
PostPosted: Mon Apr 10, 2006 9:34 am 
Beginner
Beginner

Joined: Fri Feb 04, 2005 2:44 pm
Posts: 25
Location: Buffalo, NY
Hibernate version:
2

Mapping documents:
hibernate.cfg.xml

Full stack trace of any exception that occurs:
could not find file: hibernate.cfg.xml

Hello

I have a hibernate 2 app contained in a jar file. The hibernate.cfg.xml file is also inside the same jar file. When I run, it can't find the hibernate.cfg.xml file. How can I read the xml file from within the same jar?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 10:12 am 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
We're using the same structure and it works without any problems. Is the hibernate.cfg.xml file stored in the root of the JAR file? Is it listed if you do

Code:
jar -tvf <<jarfilename>>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 10:27 am 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
I used / in the package name in specifying the location of the hibernate.cfg.xml. It looks something like :

com/companyname/db/hibernate.cfg.xml

The db package provides some db related functionality that sits on top of our model and hides the fact that we're using Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 12:24 pm 
Beginner
Beginner

Joined: Fri Feb 04, 2005 2:44 pm
Posts: 25
Location: Buffalo, NY
hibernate.cfg.xml IS in the root of the jar. My package heirarchy looks like this:

hibernate.cfg.xml
log4j.xml
oscache.properties
com
___company
___SessionManager.java - opens all Sessions
___common
______program1
_________Main.java (uses SessionManager)
______program2
_________Main.java (uses SessionManager)


The only way I've found to get it to work is to have *2* copies of hibernate.cfg.xml (1 at the root, the other in com.company along with SessionManager).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 12:41 pm 
Newbie

Joined: Tue Oct 25, 2005 12:32 pm
Posts: 13
can you show how you are calling the configure() method on the Configuration object? are you using a string like this :

configuration.configure("/hibernate.cfg.xml");


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 2:32 pm 
Beginner
Beginner

Joined: Fri Feb 04, 2005 2:44 pm
Posts: 25
Location: Buffalo, NY
Code:
    public void connect()
    {
        Configuration config = null;
       
        try
        {
            config = new Configuration();
           
            if (configFileName == null)
            {
                //read from jar file
                config.configure();
            }
            else
            {
                //read from file name passed in as program argument
                config.configure(new File(configFileName));
            }
           
            //tack on the database name to the url
            //because database is a program argument and
            //can't be hard-coded in the hibernate.cfg.xml file
            String url = config.getProperty("hibernate.connection.url");
            url = url + database;
            config.setProperty("hibernate.connection.url", url);
            sessionFactory = config.buildSessionFactory();
        }
        catch (HibernateException e)
        {
            logger.error(e.getMessage());
            System.exit(1);
        }
    }
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 4:20 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 6:23 am
Posts: 91
Assuming that you're not passing a config file name in that should be equivalent to:

Code:
config.configure("/hibernate.cfg.xml");


This will use the classloader that loaded the Environment class - maybe you have some kind of issue with multiple classloaders. If you are using the Sun JVM then starting it with the -verbose:class argument might help you to see what is going wrong.


Top
 Profile  
 
 Post subject: hibernate.cfg.xml not found: jvm -verbose:class
PostPosted: Wed Apr 12, 2006 1:46 am 
Newbie

Joined: Tue Apr 11, 2006 3:52 am
Posts: 2
I have a very similar problem to this one where the class loader can't find cfg.xml even when it is in the correct classpath. See "Jboss classpath issue hibernate.cfg.xml not found" April 11 8.28am.

I read r1ch's post and added -verbose:class to the JVM when starting Jboss, purely for debugging. Not only did this jvm option provide class loader debug info, but it fixed the whole class loader / classpath issue. I have been having major trouble with this for a week, as have others on the web. It now looks like this is caused by the JVM or Jboss not providing correct classpath info to hibernate when it loads resources.

Code:
see  net.sf.hibernate.cfg.Configuration. getConfigurationInputStream ()

see also  java.lang.ClassLoader. getResourceAsStream(String name) ..


Thanks r1ch.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.