No, resources work perfectly well in a JAR. Just don't treat a resource as a File, because it isn't one.
Instead, use the getResourceAsStream() method and you'll have an InputStream from which you can read the contents of the resource.
posted 3 weeks ago
I see. I also have an important directory structure under the resources folder and use the methods in File to find all files under that directory. I guess this isn't something that can be done with an input stream - so is this type of use case just unsupported then? I can always just find a different way, I guess there's no reason I can't just put the resources folder in the root directory.
Hmmm... there's a class java.util.JarFile which looks like it might support that kind of thing. But then you would have to know that your resources were in a JAR and you'd have to know where the JAR was located with respect to your code. I suppose treating the JAR as a resource might work for locating it, but writing code to extract the real resources from the JAR strikes me as not being something you want to do. Sort of an ugly workaround, really. But check it out and see if it doesn't stink too badly for your needs.
I would just put another file in your resource folder that acts as a manifest: list the names of all the resources you're interested in and loop over them to load the actual resources.
posted 3 weeks ago
Interesting - those both sound like great ideas. Thanks!
I think for now I am just going to have the files not be resources, but rather just go in the root directory of the application. They are just game configuration/graphics files so there's nothing sensitive. When/if I release it, I figure I will look into one of those solutions.
When I get to that point, I'll probably post the solution here if I make one.
Stephan van Hulst wrote:I would just put another file in your resource folder that acts as a manifest: list the names of all the resources you're interested in and loop over them to load the actual resources.
One might even use a resource (properties) file.
Though remember, a "file" inside a JAR isn't a file. It's a resource, technically speaking.
"privilege" comes from the Latin words for "private" and "law" (legal) and dates to feudal times. To "claim privilege" meant that you were above the laws that applied to the common people.
posted 2 weeks ago
Ok, so I actually did this sooner than I thought I would
If anyone else is having the same problem as me, here is the full solution I made. This will create a file called RES-INDEX in the resources directory which stores paths to all other resources. It uses this file when trying to discover resources to load. Now I can use my code in jar as well as in IntelliJ.
Then you make your code call ResourceIndexer on the pre-package step of Maven Package: