5

I'm having difficulty trying to load a resource file from an external module's resource folder. I am using IntelliJ and our project is built using Maven.

The original code, prior to using modules was as such:

getClass().getResourceAsStream(rulesFile)

This now results in a null result.


The structure of the modules is as such:

client
    - java
        - org.example.client
            - Controller.java

data
    - java
        - ...
    - resources
        - org.example.data.files
            - rulesFile

In module client:

getClass().getResourceAsStream(rulesFile) does not work.

However, if I try and do ModuleLayer.boot().findModule("data").get().getResourceAsStream(rulesFile), then it works fine.

My question is, is it possible to make the rulesFile visible to the Controller.java inside the client module so that a normal class loader can be used? I would like to avoid having to explicitly specify the module name it is coming from as similar code to load files from the data module is used elsewhere.

What options do I have to avoid having to explicitly giving the module name?

I have tried adding the following to the data module file, with no success:

opens org.example.data.files to client

Thanks for the help!

Abubakr
  • 312
  • 2
  • 14
  • 1
    Maybe this can help https://stackoverflow.com/questions/46861589/accessing-resource-files-from-external-modules – Lemmy Oct 13 '20 at 20:48
  • Thanks for sharing. I looked over that before but it wasn't exactly what I was looking for. – Abubakr Oct 15 '20 at 22:37

3 Answers3

2

http://maven.apache.org/pom.html#Resources

add in your client module pom.xml:

<project>
    ...

    <build>
    ...

        <resources>
            <resource>
                <directory>../data/src/main/resources/org/example/data/files</directory>
                <includes>
                    <include>rulesFile</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
vincendep
  • 528
  • 4
  • 10
  • That's an interesting solution. Thanks for sharing - however, I'd like to avoid having to specify the paths of the files explicitly to the packages, else I could just get them from the model. – Abubakr Oct 15 '20 at 22:40
  • You could just have directory pointing to resources ../data/src/main/resources/ – Avik Kesari Oct 19 '20 at 15:00
  • That’s true, a possible solution. Thanks! – Abubakr Oct 21 '20 at 01:10
0

The structure of the modules is as such:

client
    - java
        - org.example.client
            - Controller.java

data
    - java
        - Data.java
    - resources
        - org.example.data.files
            - rulesFile

For now, maybe not the most ideal solution, but it seems that you can load from the data module when working from a Class inside the data module.

It is also important to note I have opened the data resources folder to the client module as well.

Rather than trying something like:

Controller.class.getResourceAsStream(rulesFile)

If you do:

Data.class.getResourceAsStream(rulesFile) it works, this is because Data.java is inside the data module, where the rulesFile resides.

Abubakr
  • 312
  • 2
  • 14
0

In a Java .ear type package artifact, this also happens where you need the classpath of one module to include another module. In that case, marking the necessary module as a dependency in the "wanting" module should incorporate it into the classpath of the "wanter". Specifically, this occurs with wars needing to access ejbs in an ear.

You would add the module simply by artifact name to the dependency section of the "wanting" module's pom.xml:

<dependencies>
  <dependency>
    <artifactId>data</artifactId>
  </dependency>
</dependencies

The other attributes of the artifact are inferred by your parent pom in Maven so you don't need to mention version, or groupId. Scope could be cited as runtime but I think that may be default anyway.

If all goes well, you should be able to just do getClass().getResourceAsStream(rulesFile).

dan
  • 741
  • 9
  • 14
  • The client module does have a dependency on the data module. Before our switch to Java 11, this did the trick and the class path didn’t matter for loading of the resources. However, it doesn’t seem to work now with the module setup. – Abubakr Oct 21 '20 at 01:07