getResource giving a null URL


apply plugin: 'java'

dependencies {
testCompile 'junit:junit:4.12' 

project dir is: src/main/resources. I created directories “firstSet” and “secondSet” under src/main/resources.


public static Reader getFileReader(String subdirectory, String filename) {
    String path = subdirectory +"/" + filename;
    FileReader reader = null;
    try {
    URL url = MyParser.class.getClassLoader().getResource(path);
    String path = url.getPath();
    File resourceFile = new File(path);
    reader = new FileReader(resourceFile);
    } catch (Exception e) {
    return reader;

I have a subdirectory in resources that I pass into this method as subdirectory. Url is null, I need it to find myText.txt in the subdirectory, how can I accomplish this?

The debugger shows path is “firstSet/myText.txt” It can find it if I remove the subdirectory, but I need subdirectories under the resources dir because I can have different resource files with the same name

I realized this code works if you paste it into a new gradle project inside intellij. However:

  1. If this is a second module
  2. Inside an android studio project, with the default “app” module

It does not. Why?