• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

where do I put the resources?

 
Ranch Hand
Posts: 176
Mac Chrome 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
     
    Sheriff
    Posts: 22002
    107
    Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
    Mac Chrome 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.
     
    Marshal
    Posts: 25835
    69
    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
    Mac Chrome 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
    Marshal
    Posts: 25835
    69
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yup.
     
    Ranch Hand
    Posts: 326
    Android Mac OS X Firefox Browser
    • 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.
     
    To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic