简体   繁体   中英

How to Access hibernate.cfg.xml, config.properties, and log4j.properties from outside project folder

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 :)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM