Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent: cannot find property file in war when packaged in ear

 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a war file packaged within an ear file. When I try to read a property file in a servlet using ResourceBundle, I get an exception as the property file cannot be found. However, when i deploy the same war alone (w/o packaging it as ear), the war classloader can find the property file and load properties. The property file is placed under WEB-INF/classes folder. And I am using weblogic 7.0.2 app server.

Why can't ear classloader find this file?
what can i do to solve this problem? Can anybody help please?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well no, the EAR classloader won't be able to see a property in the WAR JAR file. Are you sure you need to load the property in the EAR, or can you limit it to the WAR?

There may be something I'm missing here...
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah I have to load the property file some how so that the servlet can read it. Now when I simply doploy the war, war classloader can locate the file in web-inf/claases directory. However, when the war is packaged along with some server side componenets in an ear, then the ear classloader does not delegate this task to war classloader and it itself cannot locate the file web-inf/classes directory.

Now, do i have to set some class path or lib path in the appliocations.xml in the ear so ear classloader can find it? Or is there any other way out?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the EAR need to load the properties? If only the WAR needs them then leave the WAR ClassLoader loading the classes. There are ways to get the EAR assing the WAR ClassLoader and thereby making the properties available at the EAR level, but better not to if you don't have to.
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
Does the EAR need to load the properties? If only the WAR needs them then leave the WAR ClassLoader loading the classes. There are ways to get the EAR assing the WAR ClassLoader and thereby making the properties available at the EAR level, but better not to if you don't have to.


thanks for the reply David. however, i am still not clear.
The war needs the property file and not ear. The file is at WAR level (web-inf/classes directory). and it is not needed to access it at ear level. Now, i dont know, which classloader fails to access it when i package the war into an ear.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you loading the properties file? Are you sure you're using the correct ClassLoader? As a bit of a cheat, I tend to use a Class that is attached to the WAR to find the WAR ClassLoader and then the properties file. eg if MyServlet is packaged in the WAR, use MyServlet.class.getClassLoader().getResource(...etc...) (of course this can also be reduced to class.getResource(...))

You may find that the problem was in the way you were trying to specify a ClassLoader. Does this sound possible, or am I on the wrong track?
 
Kunaal A Trehan
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Try this option.
From the servlet get ServletContext object.
Then use method getResourceAsStream("WEB-INF\folder-name\file-name")
U will get the input stream for that file

Try this option.It shd work.

Rgds
Kunal
 
Senthil B Kumar
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i was able to find the reason, just wanted to share it..

as mentioned in J2EE API Specification, the function

getServletContext().getRealPath("/WEB-INF/eos.xml");

will fail and return null when the resource is inside a war file...

so thatz why its failing in my case....

so i tried using

getServletContext().getResource() this has a new problem..

each time.. the application is access the WLS server extracts the ear to the wldonotdelete folder and creates folder each time the server is started and when the application is accessed first time.

so i cant give

getServletContext().getResource("/eos.xml")

because... when this xml is extracted.. its named as eosXXXXX.xml , where this XXXXX is some random number.. each time..

so is there anyother solution ??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic