Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where do I put the resources?

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone.

I'm trouble figure out a "good-practice" repository to place all my resources. I generally put them in a folder somewhere in src and I get a hold of them by doing something like this:

It works this way... even in a JAR file which is good. But it really doesn't seem like good practice for 2 reasons:
  • Mixing code and resources together is obviously messy.
  • Eclipse copies the resources to the bin folder as well... real bad


  • So... where do I put my resources folder and how do I get a hold of MyResource.png? And needless to say: it should work in a JAR too.

    Thank you
     
    Rob Spoor
    Sheriff
    Pie
    Posts: 20667
    65
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Olivier Legat wrote:
  • Mixing code and resources together is obviously messy.

  • If you really think this is a problem, put the resources file in the root source folder. You will then have one or more file trees with the source / class files, and one file tree with the resources.

  • Eclipse copies the resources to the bin folder as well... real bad

  • Why is that bad? The resources need to be located relatively to the class files. After all, it's the class files you'll be distributing, usually as one or more JAR files. Yes, you'll have the same files in two locations, but as long as you don't touch the ones in the bin folder that shouldn't be a problem.

    So... where do I put my resources folder and how do I get a hold of MyResource.png? And needless to say: it should work in a JAR too.

    Well, in the bin folder; either as a direct folder (in which case you need to use "/resources/MyResource.png") or relative to the class. That way, when you create your JAR file, you include everything in the bin folder, including the resources.
     
    Olivier Legat
    Ranch Hand
    Posts: 176
    Chrome Mac Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Rob, thanks for your reply.

    The "Eclipse copies the resources to the bin folder" isn't that big of a deal for my case since my resources don't take much space. But I imagine this could be an issue for laaaaaaaarge scale programs (you know... like RuneScape or something).

    I mainly just want to know if there's a conventional way of dealing with resources. Because if there is then I'd rather stick to the conventions.
     
    Paul Clapham
    Sheriff
    Posts: 21416
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The existence of the "getResource" method in basic classes like Class and ClassLoader suggests to me that putting the resources in the classpath (which is what they essentially implement) is the conventional way.

    And if you distribute your application in a jar file (another conventional way of doing things) then having the resources inside the jar is the simplest and most obvious thing to do.
     
    Olivier Legat
    Ranch Hand
    Posts: 176
    Chrome Mac Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Fair enough. I'm convinced.

    Good to know I was right all along
     
    Paul Clapham
    Sheriff
    Posts: 21416
    33
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yup.
     
    Ove Lindström
    Ranch Hand
    Posts: 326
    Android Firefox Browser Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If it is a lot of resources that is needed, such as images and stuff, I sometimes divide it into two projects, say MyApp that produce the MyApp.jar and the MyAppResources that produces the MyAppResources.jar.

    Then it is quite possible to just create a copy of MyAppResources when you need to do a new skin on the app and just switch the resource-jars. Sometimes we even have language specific resources in language coded projects, i.e. MyAppResources holds the base language and MyAppResourcesSE holds everything that has to do with the Swedish version.

    In the end, it is all down to where to find the resources in the classpath.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic