Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to reference resources within EAR ?

 
John Luckcuck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody know how to reference a directory packaged within an EAR file other than using a System property to manualy specify the absolute location of the installed EAR. In my code, I want to load a properties file in a config directory packaged within my WAR.
If I do a 'new File("config/config.props")' the code looks in the bin directory of my appserver! I cannot seem to reference file resources within my EAR.
Please Help
Cheers,
John
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An option that is open to you is to load such resources with the ClassLoader, for example, using getClass().getResource("/myResources.properties"). This means that you can then simply include resources inside your JAR files.
Of course, if you are loading resources from a webapp within an EAR file, you can also use getServletContext().getResource() if your resources are underneath the WEB-INF directory of your web application.
Using a system property to directly reference the deployed location of an EAR file is not the best thing to do, simply because the app servers are free to copy and unextract the contents into a directory on deployment.
Hope that helps
Simon
 
John Luckcuck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon, your a star ! I'd tried the class.getResource("") method before but it kept giving me the runtime of the appserver, but the getServletContext().getResource() works fine. Many thanks !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic