ClassLoader.getResource() cannot load resource from jar dependency

 Windows 10,  java version "1.8.0_201" Java(TM) SE Runtime Environment (build 1.8.0_201-b09) Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode) 

I have Maven project with the following structure:

 ResourcesApp/ ├── pom.xml ├── resources │   ├── pom.xml │   └── src │       └── main │           ├── java │           │   └── pl │           │       └── sgnsajgon │           │           └── resource │           │               └── utils │           │                   └── Utils.java │           └── resources │               ├── config │               │   └── my-config.xml │               └── my-data.xml └── resources-consumer     ├── pom.xml     └── src         └── main             └── java                 └── pl                     └── sgnsajgon                         └── resourcesConsumer                             └── EntryPoint.java 

I want to load resource files from resources module (my-data.xml, config/my-config.xml) in code within resources-consumer module. The code of EntryPoint.java is:

 package pl.sgnsajgon.resourcesConsumer;      import java.net.URLClassLoader;     import java.util.Arrays;      import pl.sgnsajgon.resource.utils.Utils;      public class EntryPoint {          public static void main(String... args) {              System.out.println("Run method from resource dependency module to ensure it's loaded :");             Utils.sayHello();              System.out.println();              String[] files = { "my_data.xml", "my_config.xml", "config/my_config.xml" };              System.out.println("using Class.getResource()");             for (String file : files) {                 System.out.println(file + ": " + (EntryPoint.class.getResource(file) != null ? "found" : "NOT found"));             }              System.out.println();              System.out.println("ClassLoader URLs:");             Arrays.stream(((URLClassLoader)EntryPoint.class.getClassLoader()).getURLs()).forEach( url -> System.out.println(url));              System.out.println();              System.out.println("using ClassLoader.getResource()");             for (String file : files) {                 System.out.println(file + ": " + (EntryPoint.class.getClassLoader().getResource(file) != null ? "found" : "NOT found"));             }         }     } 

I know the difference between Class.getResource() and ClassLoader.getResource() methods. I expect the first method to not work, but as I understand loading mechanism, the second method should work.

When I run code in Intellij Idea (using Application run/debug configuration), I get:

      Run method from resource dependency module to ensure it's loaded :     Hello      using Class.getResource()     my_data.xml: NOT found     my_config.xml: NOT found     config/my_config.xml: NOT found      ClassLoader URLs:     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/charsets.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/deploy.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/access-bridge-64.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/cldrdata.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/dnsns.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/jaccess.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/jfxrt.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/localedata.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/nashorn.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/sunec.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/sunjce_provider.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/sunmscapi.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/sunpkcs11.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/ext/zipfs.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/javaws.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/jce.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/jfr.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/jfxswt.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/jsse.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/management-agent.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/plugin.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/resources.jar     file:/C:/Program%20Files/Java/jdk1.8.0_201/jre/lib/rt.jar     file:/C:/Users/sgnsajgon/Repositories/ResourcesApp/resources-consumer/target/classes/     file:/C:/Users/sgnsajgon/Repositories/ResourcesApp/resources/target/classes/     file:/C:/Users/sgnsajgon/AppData/Local/JetBrains/Toolbox/apps/IDEA-U/ch-0/191.6183.87/lib/idea_rt.jar      using ClassLoader.getResource()     my_data.xml: NOT found     my_config.xml: NOT found     config/my_config.xml: NOT found      Process finished with exit code 0  

You can see that I have in ClassPath the following directory where copies of resource files are located after Maven build:

 file:/C:/Users/sgnsajgon/Repositories/ResourcesApp/resources/target/classes/ 

but Class Loader cannot find those resource files.

When I run code in console using the following command:

 java -cp "resources-consumer/target/resources-consumer-1.0-SNAPSHOT.jar;resources/target/resources-1.0-SNAPSHOT.jar" pl.sgnsajgon.resourcesConsumer.EntryPoint 

I get:

      Run method from resource dependency module to ensure it's loaded :     Hello      using Class.getResource()     my_data.xml: NOT found     my_config.xml: NOT found     config/my_config.xml: NOT found      ClassLoader URLs:     file:/C:/Users/sgnsajgon/Repositories/ResourcesApp/resources-consumer/target/resources-consumer-1.0-SNAPSHOT.jar     file:/C:/Users/sgnsajgon/Repositories/ResourcesApp/resources/target/resources-1.0-SNAPSHOT.jar      using ClassLoader.getResource()     my_data.xml: NOT found     my_config.xml: NOT found     config/my_config.xml: NOT found  

You can see that I have in ClassPath the resource module jar file (resources-1.0-SNAPSHOT.jar) containing resource XML files, but Class Loader cannot find those resource files.

I have no idea why it doesn’t work. Please help