Loading a resource other than Java from WAR

I am trying to load a non-Java resource from a specific WAR file. Here is how I am trying to achieve this:

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

Works well when working 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 turn cannot find the resource inside this WAR.

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

  • Jboss 4

     "file:/C:/path/to/my/WarFile.war/" 
  • Jboss 6

     "file:/C:/jboss6/bin/a1k2347-kpm5pr-hjfoi81u-1-hjfoj582-dz/" 

    "/ a1k2347-kpm5pr-hjfoi81u-1-hjfoj582-dz /" does not even exist.

I have the feeling that I did not configure my jboss6 correctly or that I missed another important thing here. Any ideas what could happen? Or maybe even someone who can explain what I'm doing wrong?

+4
source share
1 answer

Well, it looks like I lack a basic understanding of how WAR files should work.

After I realized that only WEB-INF/classes and WEB-INF/lib were added to the classloader class path, and NOT was the root directory of the WAR file, the solution was quite simple:

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

This is really ugly. Someday I will spend some time improving our assembly procedure, so the resources will automatically go to the directory located on the class path.

+4
source

Source: https://habr.com/ru/post/1492710/


All Articles