简体   繁体   中英

How does Eclipse IDE resolve File/Resource location?

My question is expressed in terms of these

two Images.

In the first one, configuration.configure() method of hibernate loads the

file from src folder as indicated by red arrows

Eclipse映像1

While in this second image,

it is shown that PropertyConfigurator.configure() method of Log4j loads the file from

root folder of Project as indicated by the red arrows again

Eclipse映像2

In both projects, this and only this setup works and no alternate setup works.

Thanks for help.

Eclipse isn't doing anything, it's all the JVM. the results would be the same if you used the command line or another IDE.

The different programs load configuration diferently in your 2 examples.

  1. Hibernate loads config files as a resource which looks for a file with that path on your class path which is why it can be in src .

sourcode of Hibernate's Configuration#configure(String)

  1. Log4j uses the String as a path to open a FileInputStream . Since you give a relative path, it looks for a file relative the root of your project

sourcecode of log4j PropetyConfigruator#configure(String)

load hibernate.cfg.xml

configuration.configure(hibernate.cfg.xml) means your hibernate configuration file “hibernate.cfg.xml” is at the root of your project classpath.Even if you don't pass the configuration file name in the configure() method, it will work, but in that case name should be hibernate.cfg.xml.

If you want to place this configuration file into a different directory then you need do like below

new Configuration().configure("/com/config/hibernate.cfg.xml")

load log4j.properties

Loading log4j.properties also works in the similar way.

If you want to put your log4j.properties in to com-->config source folder you can do like below

PropertyConfigurator.configure("classpath:com/config/log4j.properties"); 

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