I was searching for this problem on internet but did not find anything that would specifically dealing with this (there were some getResources()
posts but nothing like my problem) so I am asking this question here now:
I am using Netbeans8.0.2 and would like to use [MY_CLASS_HERE].class.getResources()
to include some file(s) in the JAR and loading it from inside of it but for some reason it only works with images and nothing else, like if I have:
SomeClass.class.getResource("/someImage.jpg");
SomeClass.class.getResource("/someImage.png");
SomeClass.class.getResource("/someImage.gif");
...
All that above works just fine and as expected: it does not put it to any folder during project build - it just includes it to my JAR file. But if I enter anything else than image file, for example like this:
SomeClass.class.getResource("/someFile.obj");
It simply complains that it cannot find the file like this:
java.io.FileNotFoundException: file:\Z:\JAVA\MYPROJECT\build\classes\someFile.obj (The filename, directory name, or volume label syntax is incorrect)
I have all the files (images and all the other types I want to use this way) in the exact same folder "resources" inside my project in NetBeans...
Strange cos why is it also not complaining about the images then?
Why is that and how to solve this - can anyone tell,please?
UPDATE:
@Jerome's suggestion works as for the successfull loading of any other file type then image by using his code:
Someclass.class.getRessourceAsStream("/someFile.obj");
It loads the file BUT another problem is that I need String representation of the path so when I use:
Someclass.class.getRessourceAsStream("/someFile.obj").toString();
...then it throws another error as it cannot read the file content because the string interpretation of the path looks like this:
java.io.BufferedInputStream@215d7ea7
...where of course my code is expecting something like normal path string, like:
"JARFILE/resources/someFile.obj"
So my updated question is: how to interpret it now as a normal path string? Or should I start another (new) topic about it?
To explain it even more: I need a string path to my .obj file for a parameter attribute like this:
api.parameter("filename", "obj/someFile.obj");
Above example worked in a previous version of my app where I had that .obj file placed in a folder called "obj" in the same directory as my .jar file, but now as I am trying to rather include it in the JAR itself with code:
api.parameter("filename", Vectors.class.getResourceAsStream("/someFile.obj").toString());
...it is not working anymore (as the path string interpretation is not a path to a file), and I am trying to find a solution to this "path string" mess.
Try to use Someclass.class.getRessourceAsStream(...)
or ClassLoader.getSystemClassLoader().getResourceAsStream(...)
But be careful it will return a InputStream
and not a URL
.