This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes where do I put the resources? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "where do I put the resources?" Watch "where do I put the resources?" New topic
Author

where do I put the resources?

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

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


    Olly
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19651
        
      18

    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.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Olivier Legat
    Ranch Hand

    Joined: Nov 17, 2007
    Posts: 176

    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
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    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

    Joined: Nov 17, 2007
    Posts: 176

    Fair enough. I'm convinced.

    Good to know I was right all along
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Yup.
    Ove Lindström
    Ranch Hand

    Joined: Mar 10, 2008
    Posts: 326

    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.
     
    jQuery in Action, 2nd edition
     
    subject: where do I put the resources?
     
    Similar Threads
    if a use a folder my code cant read the path
    Adding To An ArrayList
    How to get the running jar path?
    SQL Exception:General Error
    Images in jar