aspose file tools*
The moose likes JBoss/WildFly and the fly likes Access resource that lives in a different war Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Access resource that lives in a different war" Watch "Access resource that lives in a different war" New topic
Author

Access resource that lives in a different war

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: May 14, 2008
Posts: 5843
    
    7

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.

JBoss In Action
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: May 14, 2008
Posts: 5843
    
    7

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

Joined: Dec 21, 2009
Posts: 102
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

Joined: May 14, 2008
Posts: 5843
    
    7

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access resource that lives in a different war