0

I tried to make one of my projects Modular, but if I do, I can't access my Resource files anymore.

How can I debug or declare which "Resources" my project should Use, within my "module-info.java"

Or is there a "moduleLoader" for the resources ?

Here is what I tried: Accessing resource files from external modules

Also My Code that prints true if it finds my resource. This is true, as long as my project is not modular. As soon as I create a module-info.java this is false.

   public class Launcher {
    public static void main(String[] args) {
       System.out.println( Launcher.class.getClassLoader().getResourceAsStream("test.txt")!=null);
    }
}

my Module Info:

module myModule{
    opens com.project.main;
}

As soon as I remove my module-info.java, I can find the resource. Any explanation for this behavior?

Also my project Structure:

src
  '--main
  |'--java
  |  | '--com.project.main
  |  |   '--Launcher.java
  |  '--module-info.java
  '--resources
    '-test.txt
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Luxusproblem
  • 1,913
  • 11
  • 23
  • Put your resources into the package that reads them, i.e. under src/main/resources/con/project/main/test.txt. – Axel Apr 18 '20 at 16:58
  • @Axel This does not work. Or should i consider creating a package in my "module" to access the resources without the classloader? – Luxusproblem Apr 18 '20 at 17:01
  • What build tool are you using? If using grade or maven, it should work (don’t include the path in the call to getRessourceAsStream). If you compile directly from your IDE, it might help to put test.txt in the same folder as Launcher.java. – Axel Apr 18 '20 at 18:12
  • BTW you don’t need the getClassLoader() call. – Axel Apr 18 '20 at 18:13
  • @Axel I use Gradle. But I want to have static ressources that should be in the resources folder. I don't understand the "point" of modularity if, I can't define a resource path for my "modules" or that I can use the "global" resources for my Project. – Luxusproblem Apr 18 '20 at 18:18
  • I don’t understand your problem. Either your resources belong to a module, then add them to the module by placing them in a package that’s inside that module. Or alternatively put them anywhere on the file system. But then you don’t use a classmate to access them. – Axel Apr 18 '20 at 18:52
  • @Axel For my understanding each module should have its own resources. And for my Understanding of the static resource structure of any Java Project build/deployment, I should access Resources via "classloader" wich locates given resources for any given project. So each module has a resources folder, that should be accessible via a "resource classpath" I tried to orient my self by the "https://github.com/javafxports/openjdk-jfx/tree/develop/modules/javafx.controls/src/main/java" JavaFX implementation, but its a mystery how each "module" accesses its own resources form the "resource folder" – Luxusproblem Apr 18 '20 at 20:12
  • If you create a minimal example project on GitHub, GitLab or wherever that I can check out and just build and run using Gradle, I will try it out and send you a patch or PR (assuming that I get it to work - it does in my own projects, so why shouldn't I...). – Axel Apr 19 '20 at 07:18
  • @Axel https://github.com/Luxusproblem/SimpleJavaFXBuild Here is my Repository feel free to check out. Thank you very much for your time investment. It requires Java 11. You can build a Jar, and it should run, and also then "load the resources vial classloader". I tried to document some parts. Its just two classes. I hope you will find the issue. – Luxusproblem Apr 19 '20 at 07:58

0 Answers0