Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, where in the package/source hierarchy should resources be placed?

Say I have developed a game, and placed it in the package structure:

com.dxmio.games.breakout

Where then is the 'best practice' place to put resources like audio and images that the game uses?

like image 673
dreadwail Avatar asked Jun 23 '09 02:06

dreadwail


People also ask

Where are Java packages located?

The classes belonging to the package java. awt are stored in directory " $BASE_DIR\java\awt\ " while the classes of package java. awt. event are stored in directory " $BASE_DIR\java\awt\event\ ".

Are Java packages hierarchical?

The Java 2 SDK organizes its vast collection of classes and interfaces into a tree-like hierarchy of packages within packages, which is equivalent to directories within directories.

Which is the default Java package that will be auto included in the classpath while compiling and running a Java program?

Java compiler imports java. lang package internally by default.

Is a package just a folder Java?

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code.


2 Answers

You can always adopt a standard Maven approach to your project and put all application source files in:

{home}/src/main/java/com/dmxio/games/breakout

and then your resources live in:

{home}/src/main/resources/com/dmxio/games/breakout

and your tests then live in:

{home}/src/test/java/com/dmxio/games/breakout

This structure is a standard convention to structuring projects in the Maven world. It brings a lot of advantages with it, which you may want to look at. You can check out the resources section here: Apache Maven Guides - How do I add resources to my JAR

Alternatively :) the other answer's approach here is just fine...

like image 89
Jon Avatar answered Sep 30 '22 14:09

Jon


I have seen this handled in a number of different ways:

  • Place your resources directly in a subdirectory under com/dmxio/games/breakout (e.g. /com/dmxio/games/breakout/images/foo.gif and /com/dmxio/games/breakout/images/bar.gif)

  • Place your resources in a jar along with your class files (e.g. foo.gif and bar.gif bundled in breakout.jar)

  • Place your resources in a separate 'resources jar' in a subdirectory under com/dmxio/games/breakout (e.g. foo.gif and bar.gif bundled in /com/dmxio/games/breakout/images/images.jar)

I tend to favor the last option.

You can then use the java.lang.Class.getResource() method to retrieve your resources.

like image 32
Brandon E Taylor Avatar answered Sep 30 '22 15:09

Brandon E Taylor