In Java, should I create a new Package, Folder or Source Folder?

There are a few questions about SO that look like this, but I'm completely new to Java development, and I don't know how to get it right.

I have a C # solution containing two projects (my application and unit test project) and in the application, most things are placed in folders, for example. Interfaces, Exceptions, etc.

I am trying to recreate this in Java / Eclipse, but I do not know how to do this. As a result, I got a lot of packages that sound very bad. I also tried adding the source folder, but this turned out to be out of the package.

Can someone point me in the right direction?

Namely, which one should I use to represent my project / unit unit tests and subfolders that exist only for organizing the material.

Edit: he also says that using the default package is not recommended. What should I do?

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

My layout

+5
source share
4 answers

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

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

, , , ( ). , . , google List, com.google.List, java.util.List.

, :

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

.

. java.

. java .java( ).

Eclipse " " - , , , Eclipse Java. (, JAR) .

Eclipse , . Eclipse " ", . , com.mycompany.myproject, com, mycompany, myproject.

+2

java

foo.bar

SRC/Foo/

, - ,

+1

java . java Eclipse, maven plugin maven , -. maven , , .. this

0

Using the default package can create namespace conflicts. Imagine you are creating a library containing the MyClass class. Someone uses your library in their project, and also has the MyClass class in their default package. What should the compiler do? A package in Java is actually a namespace that fully identifies your project. Therefore, it is important not to use the default package in real-world projects.

0
source

All Articles