Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Resources in a jar file - best practices?

 
Rancher
Posts: 214
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Java application that uses a lot of resource files. Running this application in IntelliJ works fine, and finds the resources correctly using this helper:

When I package this project into a jar file and try to run it, it cannot find any of the resources. Sample error message:

(File 'file:\C:\Users\...\...-1.0-SNAPSHOT.jar!\engine_extensions\definitions' does not exist)



All my resources are in the default directory (src/main/resources) and I use Maven to package it.

Am I missing something here, or do resources simply not work for a jar file? And if this is the case, how would you recommend managing resource files like this that I need for internal use?
 
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zachary Griggs
Rancher
Posts: 214
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 25930
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 12409
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zachary Griggs
Rancher
Posts: 214
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Zachary Griggs
Rancher
Posts: 214
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic