Greetings all,
I'm attempting to create an executable jar for a standalone Java/Swing/Hibernate/MySQL app built in Eclipse/Galileo.
Everything works fine when I execute the app from within Eclipse.
I create a jarfile using the advanced features thereby creating the following ./src/manifest.mf :
Code:
Manifest-Version: 1.0
Main-Class: toad.model.TopLevelDemo
I've remembered to embed a single blank line as the last line of the file.
I then create an executable jar, clicking the "extract required libraries into generated jar" option. The resulting .jar is roughly 24Mbytes.
Double-clicking the executable jar does fire up the app and it does execute but stalls when it hits
Code:
Session session_a = HibernateUtil.currentSession("toad");
Here's my HibernateUtil.java
Code:
package toad.persistence;
import org.hibernate.*;
import org.hibernate.cfg.*;
import org.apache.commons.logging.*;
import javax.naming.*;
import java.io.*;
import java.util.*;
import java.net.URL;
public class HibernateUtil
{
private static final boolean usingAnnotations = true;
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static HashMap<String, SessionFactory> sessionFactoryMap = new HashMap<String, SessionFactory>();
public static final ThreadLocal sessionMapsThreadLocal = new ThreadLocal();
static {
try {
String fileName;
SessionFactory sf;
// Create the SessionFactory objects from *.cfg.xml
ArrayList<String> cfglist = getHibernateCfgFiles("/");
for( String keyy : cfglist) {
fileName = keyy + ".cfg.xml";
if( usingAnnotations) {
sf = new AnnotationConfiguration().configure(fileName).buildSessionFactory();
} else {
sf = new Configuration().configure(fileName).buildSessionFactory();
}
sessionFactoryMap.put(keyy, sf);
}
} catch (Exception ex) {
ex.printStackTrace(System.out);
log.error("Initial SessionFactory creation failed.", ex);
System.out.println("Initial SessionFactories creation failed.");
throw new ExceptionInInitializerError(ex);
} // end of the try - catch block
}
public static Session currentSession() throws HibernateException
{
return currentSession("");
}
public static Session currentSession( String keyy) throws HibernateException
{
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
if( sessionMaps == null) {
sessionMaps = new HashMap();
sessionMapsThreadLocal.set( sessionMaps);
}
// Open a new Session, if this Thread has none yet
Session s = (Session) sessionMaps.get(keyy);
if( s == null) {
s = ((SessionFactory) sessionFactoryMap.get(keyy)).openSession();
sessionMaps.put(keyy, s);
}
return s;
}
public static ArrayList<String> getHibernateCfgFiles( String path)
{
ArrayList<String> ret_arry = new ArrayList<String>();
String fileName, keyy;
String[] str_arry;
URL url = Configuration.class.getResource( path); // returns C:/eclipse_workspace/sais_check_a/bin
File file = new File( url.getFile());
if( file.isDirectory()) {
for( File f : file.listFiles()) {
if( f.isFile()) {
fileName = f.getName();
if( fileName.endsWith(".cfg.xml")) {
str_arry = fileName.split("\\.");
keyy = str_arry[0];
ret_arry.add( keyy);
}
}
}
}
return ret_arry;
}
public static void closeSession()
{
log.debug("closing a single session");
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
sessionMapsThreadLocal.set(null);
if( sessionMaps != null) {
Session session = sessionMaps.get("");
if( session != null && session.isOpen())
session.close();
}
}
public static void closeSessions() throws HibernateException
{
log.debug("closing sessions");
HashMap<String, Session> sessionMaps = (HashMap<String, Session>) sessionMapsThreadLocal.get();
sessionMapsThreadLocal.set(null);
if( sessionMaps != null) {
for( Session session : sessionMaps.values()) {
if( session.isOpen())
session.close();
};
}
}
/**
* Closes the current SessionFactory and releases all resources.
* <p>
* The only other method that can be called on HibernateUtil
* after this one is rebuildSessionFactory(Configuration).
*/
public static void shutdown()
{
log.debug("Shutting down Hibernate");
closeSessions();
// Close caches and connection pools
if( sessionFactoryMap != null) {
for( SessionFactory sf : sessionFactoryMap.values()) {
sf.close();
}
}
sessionFactoryMap = null; // Clear static variables
}
}
What do I need to add to do to get the executable jarfile to work with Hibernate?
TIA,
Still-learning Stuart