javajbossgetresourcewildfly-8

WildFly - getting resource from WAR


I am using the following method to get a resource from WAR file in WildFly:

this.getClass().getResource(relativePath)

It works when the application is deployed as exploded WAR. It used to work with compressed WAR, too. Yesterday, I did a clean and rebuild of project in Eclipse, and it just stopped working.

When I check the resource root:

logger.info(this.getClass().getResource("/").toExternalForm());

I get this:

file:/C:/JBoss/wildfly8.1.0.CR1/modules/system/layers/base/org/jboss/as/ejb3/main/timers/

So, no wonder it doesn't work. It probably has something to do with JBoss module loading, but I don't know if this is a bug or normal behavior.

I found various similar problems on StackOverflow, but no applicable solution. One of the suggestions is to use ServletContext like so:

@Resource
private WebServiceContext wsContext;
...
ServletContext servletContext = (ServletContext)this.wsContext.getMessageContext()
        .get(MessageContext.SERVLET_CONTEXT);
servletContext.getResource(resourcePath);

But, when I try to obtain MessageContext in this manner, I get an IllegalStateException. So I am basically stuck. Any ideas?


Solution

  • I finally gave up and put my resource files in a new JBoss module, as described in this link.

    https://community.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath

    It works, but the downside is that there are two deployment targets so things are more complicated. On the upside, the size of the WAR file is reduced, and I don't have to redeploy the application if only some of the resources have changed.