23

What is the standard project directory structure of a standalone Java SE (Command Line based) application?

src folder will contain all my .java files in properly organized packages. Other than that I have bin folder which will contain my .class files.

I've Properties files and XML configuration files in my project. In which directory should I place them? Should I create a package named com.myproject.config and place all the .xml config files in that?

I want the dependent jars to be packaged along with my final package. So should I create a folder (say by the name lib) to hold all these .jar files?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
HariShankar
  • 859
  • 1
  • 10
  • 21
  • You may do as you describe. There is no constraint nor better accepted way. – Denys Séguret Jun 12 '12 at 07:19
  • 1
    A lot of this depends on which build tool/IDE you are using. Note that if packaging it all up yourself, it is theoretically possible to use almost any structure, while an IDE will typically set up a structure that it more commonly uses. As to *"create a package named com.myproject.config"* Yes, that seems reasonable. – Andrew Thompson Jun 12 '12 at 07:19
  • 1
    I think use Maven to build your project is a good option. – plucury Jun 12 '12 at 07:20
  • BTW - is this question really more about how to **deploy** the built application to the end-user? What type of application is it? (e.g. Desktop, command line, something meant for the system tray..?) – Andrew Thompson Jun 12 '12 at 07:21
  • @AndrewThompson : Command Line application – HariShankar Jun 12 '12 at 08:17
  • 1
    I edited 'command line' into the question, but am still not clear on the answer to my first question. *"is this question really more about how to **deploy** the built application to the end-user?"* If not it seems the answer to this is 'use whatever structure that works for you'. – Andrew Thompson Jun 12 '12 at 08:36

4 Answers4

27

I would recommend to stick with default Maven layout ( and also use maven as build tool )

Productive classes / resources:

src/main/java
src/main/resources

Test data and classes:

src/test/java
src/test/resources

Maven can also take care to package your application properly with all the necessary jars ( look for maven assembly plugin )

Pau Kiat Wee
  • 9,485
  • 42
  • 40
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
8

src/com.enterprise_name.project_name. Main.java (the main class)

src/com.enterprise_name.project_name.model.(here all model classes)

src/com.enterprise_name.project_name.view.(here all view classes, JFrame, Jdialog,etc)

src/com.enterprise_name.project_name.view.resources.(here all the files and images used in the views *note)

src/com.enterprise_name.project_name.controller.(here all controller classes)

lib/(here all the external libraries - dont forget add to build path)

*note if you need some resource file (xml, config file, etc) create a package .resources. in the specific place where do you need (model, controller, view)

Musculaa
  • 934
  • 10
  • 19
5

As far I as know, there is no such thing as a standard project structure for Java per se. However, various tools and IDEs (such as Eclipse) do have a standard structure.

Maven, a build tool for Java, on the other hand has a very clearly defined (default) project structure. There, all Java source files are placed in the src/main/java folder and all resource files (like your config files) are placed in src/main/resources.

Here's a very quick introduction to Maven, if you don't know it yet: Maven in 5 Minutes


Regarding your question about packaging with dependencies, here is a snipped from one of my Maven POM files that uses the Assembly Plugin to create a JAR file with all dependencies included:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>...</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
rolve
  • 10,083
  • 4
  • 55
  • 75
5

A commonly used structure is the following:

  • src - contains all your source files, and possibly the following as well (might be far down the folder tree):
    • resources - contains resources such as properties files
    • config - everything config related
  • lib - a folder containing a your libraries, possibly placed in separate subfolderrs
  • bin - contains compiled classes
DieterDP
  • 4,039
  • 2
  • 29
  • 38