0

I want to export my java project (using eclipse) into executable jar, but I want hibernate.cfg.xml, config.properties, and log4j.properties editable for future, so how to make hibernate access that file from outside project folder or any other way to make that file editable for future,

I have try this code for acces hibernate.cfg.xml from outside project folder

SessionFactory sessionFactory = new Configuration().configure("mon/hibernate.cfg.xml").buildSessionFactory();

but i got this error

mon/hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError

and still have no idea about config.properties and log4j.properties, any help will be pleasure :)

splatter_fadli
  • 761
  • 4
  • 16
  • 32

2 Answers2

1

there is my solution to your problem:

config.properties

You define your configuration file trough parameter -DconfigurationFile set to your JVM. Then try to find confiFile in your classpath (inside jar) if is not found then filesystem will be searched. Well, as last the properties will be override with JVM parameters.

Properties prop = new Properties();
String configFile = System.getProperty("configurationFile",defaultConfigurationFile);
    try {
      InputStream classPathIo = getClass().getClassLoader().getResourceAsStream(configFile);
      if(classPathIo != null) {
        prop.load(classPathIo);
      } else {
        prop.load(new FileReader(configFile));
    } catch (FileNotFoundException e) {
      log.warn("The config file {} cannot be found. It can be setup by -DconfigurationFile parameter.",configFile);
    } catch (IOException e) {
      log.warn("The config file {} is not readable.",configFile);
    } finally {
      log.info("Configuration loaded! {} values found from configFile {}.",prop.entrySet().size(),configFile);
      prop.putAll(System.getProperties());
    }

log4j.properties

The solution is using of the following JVM parameter:

-Dlog4j.configuration={path to file}

If the file is NOT in the classpath (in WEB-INF/classes in case of Tomcat) but somewhere on you disk, use file:, like

-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties

hibernate.cfg.xml

I have no idea how to do this. Anyway, it hard to configure persistance after release because the configuration is hard bind to implementation. I think it is OK to keep it inside classpath.

Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • I am sorry, I never using JVM parameter before... have I create a new file for this code `-Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties` or where file that I have to add this code?? – splatter_fadli Oct 18 '13 at 09:33
  • I just make a Java Project not Web Project or else... in the future this java will run in the command prompt (executable jar) with this command `java -jar ` any idea?? – splatter_fadli Oct 18 '13 at 10:11
  • Then just add `java -Dlog4j.configuration=file:/somewhere/on/disk/log4j.properties -DconfigurationFile=/somewhere/config.properties -jar ` – Milan Baran Oct 18 '13 at 10:25
  • still not work for me, I got this error `log4j:WARN Caught Exception while in Loader.getResources. This may be innocuous. java.lang.IllegalArgumentException: name` – splatter_fadli Oct 21 '13 at 08:24
0

You can instruct hibernate to load config file from file system,

There are lots of overloaded configure() methods are available, see the link for the documentaion

and below is the way you can do:

File conf = new File(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml");
Configuration configuration = new Configuration().configure(conf.getAbsoluteFile());

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/cfg/Configuration.html#configure(java.io.File)

and for log4j, you can give -D argument with log4j config file

like -Dlog4j.configuration={path to .properties or .xml }

similar question for log4j externalization : How to initialize log4j properly?

It will be worth reading that as well.

Community
  • 1
  • 1
Jayaram
  • 1,715
  • 18
  • 30
  • I am sorry, I am still don't get it... where file that I have to add this code `File conf = new File(ABS_PATH_TO_CONFIG+File.separator+"hibernate.cfg.xml"); Configuration configuration = new Configuration().configure(conf.getAbsoluteFile());` into my controller or DBUtil?? – splatter_fadli Oct 18 '13 at 09:38
  • @splatter_fadli: Edited the ans, Put this where you are building your sessionfactory – Jayaram Oct 18 '13 at 11:07