Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

classLoader.getResource(filename) not returning existent file (returning null)

I'm trying to load a .json file for testing purposes on a Beam pipeline.

The code looks like

...
public class ExtractCsvMessageTest {
  @Rule public final transient TestPipeline pipeline = TestPipeline.create();

  final String filepath = "com/project/functions/ExtractCsvMessageTest/";
  final String filename = filepath + "comma_delimited.json";
  final ClassLoader classLoader = getClass().getClassLoader();

  final File commaDelimited = new File(classLoader.getResource(filename).getFile());
...

After running the debugger, I can see the line throwing the error is:

final File commaDelimited = new File(classLoader.getResource(filename).getFile());

My path looks like

test
    ├── java
    │   └── com
    │       └── project
    │           ├── functions
    │           │   ├── ExtractCsvMessageTest.java
    │           └── transforms
    └── resources
        └── com
            └── project
                └── functions
                    └── ExtractCsvMessageTest
                        └── comma_delimited.json

There are similar questions on here but I can't find anything which solved this issue.

Also, I'm using VSCode and I just created the path from resources manually.

Finally, my .classpath is

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="bin/main" path="src/main/java">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/main" path="src/main/resources">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
    <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
    <classpathentry kind="output" path="bin/default"/>
    <classpathentry kind="src" path="src/test/java" output="build/classes/test">
        <attributes>
            <attribute name="test" value="true" />
        </attributes>
    </classpathentry>
</classpath>

tl:dr

Why is

final File commaDelimited = new File(classLoader.getResource(filename).getFile());

returning null?

like image 936
mcansado Avatar asked Feb 28 '26 14:02

mcansado


1 Answers

This was a classpath issue, as suspected.

I solved it in VSCode by triggering the command palette with Cmd + Shift + P and selecting

Java: Clean the Java language server workspace

This in turn updated the .classpath file.

After exitting and launching VSCode again, everything was working again.

like image 185
mcansado Avatar answered Mar 03 '26 04:03

mcansado