1

Using the snippet below throws me a null pointer exception, but i can't find the reason:

String path = ResourcesLoader.class.getResource("src/i3d/resourcesloader/libraries/lib.txt").toString();

enter image description here

Am i using the getResources wrong? I even tried to enter the full path, like "D:\Workspace...", double backslashes, double forwardslashes but i get the exception of null pointer. The only thing i haven't tried is using java's path separator, but that shouldn't be a problem at this moment as it runs on Windows. What do i miss?

Thanks!

2
  • 1
    What about i3d/resourcesloader/libraries/lib.txt Commented Feb 27, 2012 at 14:28
  • What happens if you remove the src part from your path? Commented Feb 27, 2012 at 14:29

3 Answers 3

3

getResource searches via the classloader, so typically and simplified in the classpath. The src folder is not in the classpath – it only exists for the build. Depending on your build system (ANT, Maven, IDE internal) a resources folder may be merged into the classpath. You put your resource directly into the source folder which will also work (if the build process copies all non-Java resources to the class output folder or if the source folder is used for the output of the generated classes).

/ is the root for your resources if you use absolute resource locations. It is equivalent to the root within the src folder. /i3d/resourcesloader/libraries/lib.txt would be the correct way to access the resource.

It would be nicer to separate the resources in a separate folder that is merged by the build tool (e.g. in Maven: /src/main/java, /src/main/resources).

Sign up to request clarification or add additional context in comments.

3 Comments

/i3d/resourcesloader/libraries/lib.txt works just fine,thx.
...it looks like if i use /i3d/resourcesloader/libraries/lib.txt i can't pass the string to a file constructor because it acts like it creates an empty file, not linking with the existing one at that adress. Instead, if i use src/i3d/resourcesloader/libraries/lib.txt in the File constructor it reads the existing one. I'm confused!
getResource returns an URL. Use this, don't toString it. Better yet: In most cases you can just use getResourceAsStream and don't need any manual work with file handles.
1

It seems you are using NetBeans?

Just create a folder called src\resources, get your files inside there, and call this.getClass().getResource("lib.txt");.

Netbeans will pack that properly when building and the resources will be in the main folder inside the jar, so you can get without having to specify their folder.

1 Comment

i've tried your way but it does't seem to find the file;still null pointer. using the /i3d/resourcesloader/libraries/lib.txt works though.
1

You should only provide relative path from your classloading point (ie without src):

String path = ResourcesLoader.class.getResource("/i3d/resourcesloader/libraries/lib.txt").toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.