Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access resource that lives in a different war

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JBoss 5.1

I have two wars both deployed in the same ear. One of the wars deploys some images in images/. I want to get access to that resource from another war. Is that possible?

I am trying :


I am pretty sure that you cannot get to classes from another war but I thought you could get to things that were in the wars root like my images directory.

Thanks
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot do that. The Java EE spec states that nothing outside a WAR can have access to things inside a WAR. the correct way to fix this is move the common resources outside the WAR and place them into a JAR in the EAR.

The incorrect way to kludge this is to change the useJBossWebLoader and java2ClassLoadingCompliance properties of the WarDeployer bean in the
server/xxx/deployers/jbossweb.deployer/META-INF/war-deployers-jboss-beans.xml file. Be aware that making this change can lead to some surprises.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, that makes sense.

I have re factored my project and now I have the images in my war's root. (i.e. I do not need to go to another war to get them). However I am still having problems getting a hold of that resource.


In the debugger I can see that my classloader is something like ...../myEar.ear/myWar.war, so I have the classloader for the war in which my images live. Are there other reasons why my call to getResource("images") would return null?

This is in a Servlet class which lives in my war, shouldn't I be able to do this?
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The root of the WAR is not in the classpath. Instead, place them in WEB-INF/classes, that is in the classpath.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that good practice to put the images in the classes folder? Seems like maybe I am not doing things right. Do you/others put images in the war and try to access the like I am?

Thanks again for the help!
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on what you are doing. From the code you posted it appears that your servlets are serving images. For that specific code the images need to be in the classpath, but in general your servlets could get the images form anywhere on your server, even from a directory completely external to you WAR, or even external to the app server . I have seen people use servlets to serve images in external directories when the collection of images can change; this way it is easy to add images because the WAR doesn't have to be rebuilt.

Of course, if your HTML output includes img tags that reference the image files directly, then the image files cannot be within the WEB-INF directory because that is not accessible from outside the WAR (i.e., not from the browser).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic