I do not understand the purpose of the src folder and individual packages

I have been using eclipse only for python in the last few months and I would like to start using it for java. However, according to the tutorials I was looking at, the right way to organize your java project is to create a package in the source folder with a name like com.project , and all classes are named com.project.class , you can also create subpackages that look like to subdirectories such as com.project.utilities.* . With this convention, I don’t understand why I would create several packages for each project. Since all the code is contained in this structure, what purpose does the src folder serve?

Hope I'm just mistaken that this is the usual way to structure a Java project, because it seems rather inconvenient.

Also, I haven't cheated on this yet, but will this affect the loading of external dependencies? If I have an img folder next to the src and bin folders, do I need to use ".. \ img *" to access it?

+6
java eclipse
source share
3 answers

Yes, for a small project it may not make much sense. You could simply:

 MyProject | + - FileOne.java + - FileTwo.java + - FileThree.java 

But for large projects, you may need to divide into packages classes that relate to different functions.

For example, the main java library has (to name a few)

java.lang (contains main clans such as Object, String, Integer, Boolean, StringBuilder) java.util (contains utility classes such as List, ArrayList, Date, Map, Timer, etc.) java.io (contains classes for Input / Ouput such as File, InputStreamReader, BufferedReader, etc.

java.sql, java.swing, java.text, etc. etc.

This way you “pack together” classes that are related to each other.

The source code for these classes, by convention, in a folder named src

So you will have:

 YourProject | + - src | + packageA | + packageB 

You may also need to separate the source code from the compiled files, so the classes folder is used by convention. In addition, you may need a separate folder for hosting the libraries of the third part, another for resources, such as images, auxiliary files or others, different for documentation, etc.

Thus, a typical layout might be:

 YourProject | + - src/ + - lib/ + - classes/ + - resources/ + - conf/ + - bin/ + - doc/ + - etc/ 

But, of course, this makes sense only for large projects.

Web applications usually also contain the WEB-INF folder, etc.

If your project contains only a couple of classes, don’t worry and don’t go with one folder, but it’s good to know what the rationale is.

+9
source share

I can understand why this might seem inconvenient for small projects. However, if you have ever had to work on a project with hundreds (or thousands) of source files, having intuitive subpackages is an absolute must to keep everything organized.

As for loading external dependencies based on the relative path from where the source file is located, it all depends on how the compiled application is organized. It is not typical to refer to resources using ".. \ img" as you describe.

+3
source share

You do not need to put everything in a package. For very small applications, you can simply put the java source files directly in the src directory, which means that your classes will belong to the "default" package. But most projects use a unique package name to avoid name conflicts, for example between java.util.Date and java.sql.Date.

Using the src directory is an agreement that is easy to understand in the IDE, build tools, and other programmers. If you avoided "src" and instead used a directory structure, for example:

 com/project/MyClass.java img/icon.jpg ...etc 

Then you can’t just say that the IDE is looking for java source files in the "com" folder, because then it will interpret the class as "project.MyClass" instead of "com.project.MyClass". On the other hand, you cannot say that it is looking for source files in the root folder, because then it expects the img folder to contain java sources.

+2
source share

All Articles