Load Resource returns null with Jboss7
Solution 1
As documented on the JBoss community wiki (without using a jboss-deployment-structure.xml
file):
- Create a module for the configuration file (
jboss-as-7/modules/com/yourcompany/configuration/main/module.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mycompany.configuration">
<resources>
<resource-root path="."/>
</resources>
</module>
- Add properties to the module:
jboss-as-7/
modules/
com/
yourcompany/
configuration/
main/
module.xml
settings.properties
other-settings.xm
- Add the module to the
CLASSPATH
using aMANIFEST.MF
entry:
Manifest-Version: 1.0
Dependencies: com.mycompany.configuration
- Load a properties file from the CLASSPATH:
InputStream settingsStream =
getClass().getClassLoader().getResourceAsStream("settings.properties");
I had to create a module folder called com/mycompany/main
and add all the images in there. So this sits outside the WAR at least though within JBOSS_HOME
. Then I could load the image using:
URL imgUrl = this.getClass().getClassLoader().getResource("myimage.jpg");
Solution 2
The method this.getClass().getClassLoader().getResourceAsStream("path")
is handling the path from the root of your classpath. In a WAR this is normally WEB-INF/classes
and WEB-INF/lib
.
Your path WebContent/includes/images/
seems to be inside the root of your WAR file (which is not the classpath). So you can
- use
getResourceAsStream
ofServletContext
(see here) which handles paths to the root of the WAR file. - package all of your images in a additional JAR (e.g.
my-app.resources.jar
) and useClassLoader.getResourceAsStream()
- move your images in the classpath (see above).
Pierre
Updated on June 09, 2022Comments
-
Pierre almost 2 years
How do I load resources like images from the java code with Jboss7.1?
This used to work with Jboss4:
this.getClass().getClassLoader().getResourceAsStream("/myapp/includes/images/image1.png");
Now this returns null.
What is best practice for loading resources in java code now with Jboss7?
I did some testing:
URL url = this.getClass().getResource(""); System.out.println(url); url = this.getClass().getResource("../../../"); System.out.println(url); url = this.getClass().getResource("../../../../"); System.out.println(url); url = this.getClass().getResource("../../../../../"); System.out.println(url); url = this.getClass().getResource("includes"); System.out.println(url); 13:33:49,143 INFO [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/yyy/beans/jsf/ 13:33:49,144 INFO [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/ 13:33:49,150 INFO [stdout] (http--127.0.0.1-8080-1) jar:file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/javax/activation/api/main/activation-1.1.1.jar!/com/ 13:33:49,151 INFO [stdout] (http--127.0.0.1-8080-1) file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/sun/jdk/main/service-loader-resources/ 13:33:49,152 INFO [stdout] (http--127.0.0.1-8080-1) null