1

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.

theoneiam
  • 11
  • 6
  • Do you know Maven? – ostrichofevil Mar 27 '17 at 16:07
  • what do you mean by that? I saw it in NetBeans settings, but to be honest I have no clue what it is – theoneiam Mar 27 '17 at 16:09
  • 1
    Have you opened your jar and looked if the files are in it? – Jérôme Mar 27 '17 at 16:13
  • 1
    https://capturevision.wordpress.com/2008/06/28/how-to-embed-resource-files-using-netbeans/ – Jérôme Mar 27 '17 at 16:14
  • Maven is a build tool that makes it easier to deal with resources and dependencies. Where is your *resources* folder? Is it in your project root directory, or in *Source Packages*? – ostrichofevil Mar 27 '17 at 16:14
  • 1
    You're probably placing the files in the wrong folder. – Willian Paixao Mar 27 '17 at 16:16
  • @WillianPaixao definitelly not, then why it loads images in the same folder with no problem at all? – theoneiam Mar 27 '17 at 16:40
  • @ostrichofevil it is a standard "resources" folder in my project root directory – theoneiam Mar 27 '17 at 16:41
  • @Jérôme just checked (opened) it now and yeas, all the files are there in JAR root directory – theoneiam Mar 27 '17 at 16:42
  • 1
    Please add a dump of the all the files in the Jar file. There's something fishy here, like a misspelled path name or similar. – markspace Mar 27 '17 at 16:45
  • Habe you tried it with `Someclass.class.getRessourceAsStream(...)` or `ClassLoader.getSystemClassLoader().getResourceAsStream(...)`? – Jérôme Mar 27 '17 at 16:54
  • @Jérôme now when I use your `Someclass.class.getRessourceAsStream(...)` it doe snot show the previous error anymore (that is it seems like it finally found the file inside the JAR) but there is something else wrong cos it cannot read its structure now - in fact it is a 3D geometry that was normally loaded from a folder inside the BUILD directory, in that way it works but when I changed it to this "internal" JAR version (that is do not place it inside a folder outside a jar but rather implement it to JAR itself) it ha snow problems interpret its content...hmm – theoneiam Mar 27 '17 at 17:14
  • Try it with the ClassLoader` and use `getResouce()`. It would be great if you could update the question with some infos about you package structure in your project and if possible in the jar – Jérôme Mar 27 '17 at 17:22
  • @Jérôme ClassLoader??? Actually, I know where this new problem is - I am going to update my post, wait...and then rais another question chained with this issue... – theoneiam Mar 27 '17 at 17:29
  • @Jérôme BTW if you post your answer as separate message I would sign it as correct answer ;-) + I guess I will start new topic about the next problem I introduced in my updated initial question – theoneiam Mar 27 '17 at 18:09

1 Answers1

0

Try to use Someclass.class.getRessourceAsStream(...) or ClassLoader.getSystemClassLoader().getResourceAsStream(...)

But be careful it will return a InputStream and not a URL.

Jérôme
  • 1,254
  • 2
  • 20
  • 25
  • yes, `Someclass.class.getRessourceAsStream(...)` is correct answer, thanx + now to the nex topic chained with this problem, here: http://stackoverflow.com/questions/43053896/java-getresourceasstream-real-path-string-interpretation-of-a-file – theoneiam Mar 27 '17 at 19:19