How should I structure a Java application, where do I place my courses?

First off, I recognize just how to construct a Java application. Yet I have actually constantly been puzzled concerning where to place my courses. There are supporters for arranging the plans in a purely domain name oriented style, others different by rate.

I myself have constantly had troubles with

  • identifying,
  • positioning


  1. Where do you place your domain name details constants (and also what is the most effective name for such a class)?
  2. Where do you place courses for things which is both infrastructural and also domain name details (as an example I have a FileStorageStrategy class, which saves the documents either in the data source, or conversely in data source)?
  3. Where to place Exceptions?
  4. Exist any kind of criteria to which I can refer?
2019-05-18 22:10:28
Source Share
Answers: 7

I've actually involved such as Maven is Standard Directory Layout.

Among the key suggestions for me is to have 2 resource origins - one for manufacturing code and also one for examination code thus :


(below, both src/main/java and also src/test/java are resource origins).

Benefits :

  • Your examinations have plan (or "default") degree accessibility to your courses under examination.
  • You can conveniently package just your manufacturing resources right into a JAR by going down src/test/java as a resource origin.

One general rule concerning class positioning and also plans :

Generally talking, well organized tasks will certainly be devoid of circular dependencies. Find out when they misbehave (and also when they are not), and also take into consideration a device like JDepend or SonarJ that will certainly aid you remove them.

2019-12-03 01:55:04

I'm a massive follower of arranged resources, so I constantly create the adhering to directory site framework :

/src - for your packages & classes
/test - for unit tests
/docs - for documentation, generated and manually edited
/lib - 3rd party libraries
/etc - unrelated stuff
/bin (or /classes) - compiled classes, output of your compile
/dist - for distribution packages, hopefully auto generated by a build system

In/ src I'm making use of the default Java patterns : Package names beginning with your domain name (org.yourdomain.yourprojectname) and also class names mirroring the OOP facet you are developing with the class (see the various other commenters). Usual plan names like util , version , sight , occasions serve, also.

I often tend to place constants for a details subject in a very own class, like SessionConstants or ServiceConstants in the very same plan of the domain name courses.

2019-12-01 23:40:07

One point I've carried out in the previous - if I'm expanding a class I'll attempt and also follow their conventions. As an example, when collaborating with the Spring Framework, I'll have my MVC Controller courses in a plan called com.mydomain.myapp.web.servlet.mvc If I'm not expanding something I simply select what is most basic. com.mydomain.domain for Domain Objects (although if you have a lots of domain name things this plan can get a little bit unwieldy). For domain name details constants, I in fact placed them as public constants in one of the most relevant class. As an example, if I have a "Member" class and also have a maximum participant name size constant, I placed it in the Member class. Some stores make a different Constants class yet I do not see the value in abiding unconnected numbers and also strings right into a solitary class. I've seen a few other stores attempt to address this trouble by developing SEPARATE Constants courses, yet that simply feels like a wild-goose chase and also the outcome is also complex. Utilizing this arrangement, a huge task with numerous programmers will certainly be replicating constants everywhere.

2019-12-01 22:36:12

I such as damage my courses down right into plans that relate per various other.

As an example : Model For data source relevant telephone calls

View Classes that manage what you see

Control Core capability courses

Util Any misc. courses that are made use of (commonly fixed features)


2019-12-01 19:22:09

I assume maintain it straightforward and also do not over assume it. Do not over abstract and also layer way too much. Simply maintain it cool, and also as it expands, refactoring it is unimportant. Among the most effective attributes of IDEs is refactoring, so why not take advantage of it and also conserve you mind power for addressing troubles that relate to your application, instead after that meta concerns like code organisation.

2019-12-01 19:21:53

Class names need to constantly be detailed and also self - informative. If you have numerous domain names of duty for your courses after that they need to possibly be refactored.

Furthermore for you plans. They need to be organized by domain name of duty. Every domain name has it is very own exemptions.

Usually do not sweat it till you reach a factor where it is coming to be frustrating and also puffed up. After that take a seat and also do not code, simply refactor the courses out, assembling consistently to see to it every little thing jobs. After that proceed as you did in the past.

2019-05-19 10:38:41

Use plans to team relevant capability with each other.

Generally the top of your plan tree is your domain turned around (com.domain.subdomain) to assure individuality, and afterwards generally there will certainly be a plan for your application. After that partition that by relevant location, so your FileStorageStrategy could enter, claim,, and afterwards there could be details implementations/subclasses/whatever in and also These names can get rather long, yet import maintains them all on top of documents and also IDEs can aid to take care of that too.

Exemptions generally enter the very same plan as the courses that toss them, so if you had, claim, FileStorageException it would certainly enter the very same plan as FileStorageStrategy. Furthermore a user interface specifying constants would certainly remain in the very same plan.

There is not actually any kind of typical thus, simply make use of sound judgment, and also if all of it obtains also unpleasant, refactor!

2019-05-19 10:34:47