8

There are a couple of questions on SO that sort of hit this, but I am totally new to Java development and I don't know the correct way to approach this.

I have a C# solution, containing two projects (my app, and a unit test project) and within the app, most things are put into folders eg. Interfaces, Exceptions etc.

I am trying to recreate this in Java / Eclipse, but I don't know how. I ended up with lots of packages, which sounds really bad. I also tried adding a source folder but that ended up being outside of the package.

Could anyone point me in the right direction?

Namely, which of those should I use to represent my unit test project/set of unit tests, and subfolders which exist just for organising stuff.

Edit: It also says use of the default package is not advised. What should I be doing?

Edit 2: Here is what it looks like. Does this look vaguely correct? My original C# solution is on the right.

My Layout

NibblyPig
  • 51,118
  • 72
  • 200
  • 356

4 Answers4

5

In a typical java eclipse project, you will have one or more source folders (for example one for app code, one for your unit tests).

Each folder contains a package tree, typically starting with your base package, for example com.mycompany.myapp.

In order to avoid name collisions, packages names are usually start with the domain name of the entity who is the author of the code, starting with the top-level-domain and going backwards (more general to more specific). That way, each class fully qualified name is unique. For example if google creates a class named List, it will be known as com.google.List, and it will not enter in conflict with the existing java.util.List interface.

You can have a unlimited number of packages inside this base package, for example :

com.mycompany.myapp.persistence
com.mycompany.myapp.domain
com.mycompany.myapp.services
com.mycompany.myapp.web

It all depends on your project and the way you want to organize your code and your classes.

At the logical level, packages are named with dots as separator. They contain java classes.

At the physical on disk level, each package is a directory. The java classes are contained in .java files (most frequently one class per file).

In Eclipse a "source folder" is a folder inside your project that is known to Eclipse to contain java source files. It will be compiled included in the output (for example JAR file) when you build your project.

In Eclipse, you usually view them at the logical level, showing packages. When you tell Eclipse to "create a new package", it will create the directory for you. For example, if you tell it to create the com.mycompany.myproject package, it will automatically create a com folder containing a mycompany folder containing a myproject folder.

Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
  • So I should do what I've done above, but nest all of my packages in an initial package? – NibblyPig Apr 12 '13 at 11:33
  • Yes, basically that. Classes in your current "default package" would be directly in your project base package and the other packages would be nested inside it. Be sure to pick a package that will identify your project, not just your entity. So if you create other projects later they have a different base package. – Pierre Henry Apr 12 '13 at 11:35
  • As other mentionned, it is also advisable in many cases to use Maven as a build tool. Maven comes with default project structures for differetn kinds of projects (they are called archetypes) that separate main sources, test sources, resources and so on. These are usually very sensible, and you get many other benefits, the biggest in IMHO being dependency (JARs) management. – Pierre Henry Apr 12 '13 at 11:44
  • I see, thanks. In Eclipse, I am just trying to refactor my project but I can't seem to nest my packages. When I create a new package, it doesn't contain it within an existing package. Is there something I am missing? Or should I call the other packages basePackage.someName ? – NibblyPig Apr 12 '13 at 12:10
  • In eclipse, you can choose if you want to display packages hierarchically or flat (the default I believe). I personally always use the hierarchical option. How to change : http://stackoverflow.com/questions/3915961/how-to-view-hierarchical-package-structure-in-eclipse-package-explorer – Pierre Henry Apr 23 '13 at 17:24
1

In java source tree structure must match package structure

so foo.bar package must be laid out in

src/foo/bar

Also default package may not be advised - but you can still use it - better to put things in a package though

gheese
  • 1,170
  • 1
  • 11
  • 17
  • 1
    Usually `/src/foo/bar` means that the sources are in `/src` and classes of package `foo.bar` are in folder `/src/foo/bar` – Mark Rotteveel Apr 12 '13 at 11:19
  • 1
    Just a note, if you are familiar with C#, you can think of packages as of namespaces in C#. The concept is quite similar. – jnovacho Apr 12 '13 at 11:48
0

In java different project development structure are flowed according to type of project. So as you are new to java and Eclipse so it's better to install maven plugin and create maven project and choose a archetypes according to your project type like a standalone or web based. The maven plugin will create the project structure including packages,test packages source folder etc. You can get more about project structure from this

Krushna
  • 5,059
  • 5
  • 32
  • 49
0

Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses your library in his project and also has a MyClass class in his default package. What should the compiler do? Package in Java is actually a namespace which fully identifies your project. So it's important to not use the default package in the real world projects.

Nomesh Gajare
  • 855
  • 2
  • 12
  • 28