Proper path for using ClassLoader.getResource()?
Asked Answered
C

1

5

I've made a function(Java) that is supposed to read bytes from a file and print them to the console:

public void loadPixels(int size){
    ClassLoader cl = this.getClass().getClassLoader();
    pixels = new byte[size];
    try{
        InputStream stream = cl.getResource("res/" + fileName).openStream();
        stream.read(pixels);
        System.out.println(pixels.toString());
        stream.close();
    }catch(Exception e){
        e.printStackTrace();
    }
}

The problem is, I'm getting a NullPointerException on the line

InputStream stream = cl.getResource("res/" + fileName).openStream();

For the file I'm trying to open, the name is "font.spt", which is also the value held by fileName. This file is within the folder "res" in the project's root directory, and I'm currently using the Eclipse IDE.

Is my approach to the path for the file wrong, or is something else the issue?

To recap: fileName points to "font.spt", which is under the "res" folder in the bin directory.

EDIT: the "res" folder containing the .spt file is now under the "bin" for the project, rather than the root directory, but I still get the error. When running from the IDE or as an exported .jar, I still get the NullPointerException, where am I supposed to put these files? Can someone give me a screenshot or example?

Clink answered 2/2, 2015 at 17:54 Comment(4)
You can open the .jar file with a zipping tool and find the path where your file is or do you have a web project?Machado
The ClassLoader loads resources from the classpath. If the file is at the root of the project, it's not compiled by Eclipse to the output directory, and is thus not in the classpath at runtime. Put the file in the sources directory if you want it to be in the classpath at runtime.Skullcap
with "res/" + filename, does that mean I'd want to make a folder under "src" named "res", then put the files in there?Clink
Yes. Put it within your src folderImpressionism
D
7

By default, Eclipse will copy all non .java files it finds in the project's Source Locations to the Output Folder when it builds. So one option is to just place your resource files under the source folder along with your source code. However, a better option is to use a separate resources folder for non-Java files and declare that as an additional Source Location (via your project's Java Build Path properties). That's how Maven and Gradle projects are organized, too.

For example, you might have:

MyProject\
    src\
        java\
            com\
                ....*.java
        resources\
            fonts\
                font.spt

With both src\java\ and src\resources\ defined as Source Locations in the Build Path. Then your code to load the file would be like:

getResource("fonts/" + fileName)

Something to consider: use Gradle or Maven to manage your builds, both of which enforce/provide a project structure similar to this anyway.

Dulla answered 2/2, 2015 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.