codecab
codecab

Reputation: 111

Load non-Java resource from WAR

I'm trying to load a non-Java resource from a particular WAR file. This is how I'm trying to achieve this:

SomeClassInMyWarFile.class.getClassLoader().getResource("path/file.txt");

Works perfectly when running in JBoss 4 - where getClassLoader() returns an instance of WebAppClassLoader.

But when I try to run this code under JBoss 6, I get an instance of BaseClassLoader, which in return isn't able to locate the resource inside of that WAR.

For debugging purposes, when I call getResource("./") on both, these are the results:

I have the feeling that I didn't set up my jboss6 correctly, or that I'm missing another crucial thing here.. Any ideas what could be happening? Or maybe even someone who is able to explain what I'm doing wrong?

Upvotes: 4

Views: 2009

Answers (1)

codecab
codecab

Reputation: 111

Ok, seems like I've been missing some basic understanding of how WAR files are supposed to work.

After I understood that only WEB-INF/classes and WEB-INF/lib are added to the classloader's classpath, and NOT the root directory of the WAR file, solving the problem was pretty simple:

As a quick fix, I changed path/file.txt to ../../path/file.txt, so the path to the resource is relative to WEB-INF/classes

This is ugly, indeed. Someday, I will invest some time in enhancing our build procedure, so the resources automatically get moved into a directory that is on the classpath.

Upvotes: 4

Related Questions