1

I understand the basic structure of a java source tree & jar file:

com
  example
    mypackage
      myclass1.java
      myclass2.java

But if I have bitmaps or html files, are there conventions for where I should put them?

In the past, I've added a ui directory in the source tree root (e.g. a sibling directory from the com directory above). But this feels "sneaky": technically there could be a "ui" package.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
Jason S
  • 184,598
  • 164
  • 608
  • 970

3 Answers3

3

I prefer the Maven approach, of having a resources directory that is separate from the Java source files:

my-app
|-- pom.xml
`-- src
    |-- main
    | `--- resources <-- Resources go here (sibling directory of java)
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

If you want a more "enterprisey" approach, use the "Project Conventions for Enterprise Applications" developed quite a long time back, by Sun. AFAIK, only the Netbeans IDE implements this to some (or all) extent.

At runtime, all resources ought to be in the META-INF directory. There may be sub-directories, but I think there is enough agreement in this area, unlike source code organization.

Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • +1 for META-INF. Is there more info somewhere on how to do this? – Jason S Jun 07 '11 at 14:29
  • (I found http://stackoverflow.com/questions/70216/whats-the-purpose-of-meta-inf but it implies that there are already conventions about what should & shouldn't be in meta-inf) – Jason S Jun 07 '11 at 14:30
  • There is a convention for [SPI mapping files](http://download.oracle.com/javase/tutorial/sound/SPI-intro.html). Such files are found in `META-INF/services/`. **Edit:** I misread the previous comment. Files going into META-INF are typically resources or config files that are to be accessed using the `ClassLoader.getResource` or `ClassLoader.getResourceAsStream`. Java specifications may also agree on what files might go into a META-INF directory. – Vineet Reynolds Jun 07 '11 at 14:40
0

i don't know of any conventions. personally i put all my stuff in a data/ directory.

even if there is a package called like this, chances are very small that there is a file present which conflicts with class files.

clamp
  • 33,000
  • 75
  • 203
  • 299
0

If you use maven, it encourages/forces you to use well-adopted project structure (single WAR module):

.
├── pom.xml
└── src
    └── main
    ├── java
    │   └── com
    │       └── example
    │           └── Test.java
    ├── resources
    │   └── com
    │       └── example
    │           └── data.csv
    └── webapp
        ├── public.png
        └── WEB-INF
            ├── hidden.jsp
            └── web.xml

Few hints: placing data.csv in the same directory as Test.java package allows you to easily open this file inside Test.java (and keeping everything in order since data files are in multiple directories):

getClass().getResource("data.csv");  //will only work in com.example package

Maven will put /src/main/resources contents on your CLASSPATH automatically. /webapp subdirectory is only used for WAR files.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674