3

I have an image in my resources folder in eclipse that I use, the problem is I do not know how to include it, the absolute path is very long and getResource returns a nullpointer. I have searched google and SO and tried the usual solutions to no avail. The resources folder is in my buildpath, the class I am running the code from is in the buildpath but both of the following returns null.

System.out.println(getClass().getResource("data.txt"));
System.out.println(getClass().getResource("/resources/data.txt"));

Now trying :

System.out.println(getClass().getResource("001.txt"));

Still returns null.

System.out.println(getClass().getResource("/001.txt"));

Also null.

Right clicking the file and finding its path relative to the workspace (right-click file, properties, path)

System.out.println(getClass().getResource("/ovinger/resources/sokoban_levels/001.txt"));

Also null

Here is images of the workspace and the resources folder. The code is being run from "TestSokoban.java" Buildpath Workspace I was even worried that eclipse lacked the rights to search my desktop, run as admin, still same problem.

arynaq
  • 6,710
  • 9
  • 44
  • 74
  • Have you looked at [this answer][1]? Seems like the answer to your question. [1]: http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource – DigCamara Feb 25 '13 at 22:24
  • 2
    Worse filename for an image ever. Without knowing where it's at on the classpath it's tough to help. – Dave Newton Feb 25 '13 at 22:26
  • All of the below answers return null, I am at a loss here. Imagine projects with gazzilion images in different resource folders for organization, do I have to find the absolute path? – arynaq Feb 25 '13 at 22:57

4 Answers4

2

There is nothing easier than to find out where the root of the relative path is, by running System.out.println(getClass().getResource("."));. And then modify the path to fit the relative path properly. It should be the package, where the class is located, btw.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • 1
    Accepting this as my answer as doing this helped me find the relative path of the resources folder. Still I was under the impression that .getResource() would find any given file as long as it was somewhere in your buildpath, as the image in the OP shows that the resources folder is. – arynaq Feb 26 '13 at 12:47
0

getClass().getResource() reads from the classpath, and not the filesystem path. So, you should use package/resource notation from the root of the classpath.

Akber Choudhry
  • 1,755
  • 16
  • 24
0

Bear in mind that eclipse will execute the compiled code which is in bin folder by default or target folder if you are using maven for project. This means that you must build the project.

After performing a build, take a look at the bin folder and look up the file. That is the path that you must load.

Assuming you did that and that the resource ends up in the classpath root then the standard way would be:

ClassLoader.getSystemResourceAsStream("file.txt");

But if you are still having problems then try with:

Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");

The latter form will help you if you are executing the java class in a spawn.

ssedano
  • 8,322
  • 9
  • 60
  • 98
-1

Would you not use BufferedReader and read the file into a String?

Chris O'Brien
  • 372
  • 6
  • 25