Java: subdirectory point

First of all: I am not completely familiar with Java and some things that, as I know, I learned by playing with Java.

However, there is something that I noticed in almost any Openensource Java project - using a large number of subdirectories for sources that usually look like this:

./src/main/java/com/somedomainname/projectname/sourcefile.java 

Now, why are there so many subdirectories? what is a deal with a domain name?

+4
source share
8 answers

The domain name is used for the package name - so the file will be for the class

 com.somedomainname.projectname.sourcefile 

where com.somedomainname.projectname is the package.

Typically, the organization of the source file reflects the structure of the package. The regular Java compiler does not actually use the directory structure (although some IDEs, such as Eclipse, will complain if you put things in the β€œwrong” directories), but this forces the public classes to be in the file with the same name. Non-public classes can go in any file, but usually the file name matches the class name. This allows you to easily move to any class without any prior knowledge.

The Java language specification does not mean that the compiler should apply the convention for public classes; he clearly says he can, though. See section 7.2 JLS for more details.

+8
source

This directory structure is used as an agreement that shows where the library is and separates it from other sources.

One reason for using this structure is the standard used by Maven .

Maven is a build tool that helps you manage project dependencies. Maven is for configuration purposes, so you will often see this directory structure for it to work with Maven.

Maven points out that the directory structure starts with / src / main / java for Java files, and the rest is based on a naming convention for namespaces.

Using a domain name in a path is to prevent class collisions. If two different libraries provide a class with the same name, the domain namespace allows them to use both of these files.

+3
source

The Java package is a mechanism for organizing Java classes into namespaces similar to Modula modules. Java packages can be stored in compressed files called JAR files, allowing classes to load faster than a group, rather than one at a time. Programmers also typically use packages to organize classes into the same category or to provide similar functionality.

... from http://en.wikipedia.org/wiki/Java_package

+1
source

as an organizational tool so that you do not have only one directory with a bunch of java files. The reason you often see a domain name is because people usually get java package names from their domain names to prevent collisions with other developers. Therefore, although we both can have a util.Stringutil class, if I call my com.mydomain.util.Stringutil, and yours name com.yourdomain.util.Stringutil, we can have a project containing both classes without collision.

+1
source

It is interesting to read java packages and directories in the new O'Reilly Java book : The Good Parts (starting at the bottom of page 46).

... the required interaction between the package system and the file system is both regrettable and painful ...

+1
source

This is implied as a standard for determining unique locations for Java source code. This is consistent with this package structure, so you see it everywhere. You don’t need to do this - you can name your packages as you like. However, this agreement should be observed very often.

  package prefix.organization.project.ClassName;
 package prefix.organization.project.package.ClassName;
 package prefix.organization.project.package.subpackage.ClassName;

When storing Java source code files, each part of the package name is translated into a subdirectory. Thus, the same three classes shown above will be located in the corresponding directories from the main class path.

  prefix / organization / project / ClassName.java
 prefix / organization / project / package / ClassName.java
 prefix / organization / project / package / subpackage / ClassName.java

When compiling manually, make sure that the main directory of the path classes is the current directory or is inside the class path so that the source code files can be found.

As for the src/main/java , it seems to come from Maven. I have never used this software. I don’t understand why they need so much, because my projects (I use Eclipse) instead have the src folder instead.

0
source

./src/main/java/com/somedomainname/projectname/sourcefile.java : Unfolded

  • Src / home / java
    this is the directory to be passed to the javac compiler, which indicates the source code to compile.
    1.1 src / test / java
    this is where the unit test classes should be saved.
    1.2 src / main / resources and src / test / resources
    These are the appropriate directories in which resources such as property files should be stored. 1.3 Separate output catalogs.
    main and * test classes and resources must be compiled into their own output directories. Maven uses target / classes and target / test classes. When you combine compiled class files for distribution, you do not want to include test classes and test resource files.
  • com / somedomainname / project_name
    this directory structure corresponds to the package declaration in the classes found in the project name ie package com.somedomainname.projectname
  • SourceFile.java matches the class name that it defines, and it should start with an uppercase character by default, see http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
  • Also in the link above you will find out that the default package naming convention uses the domain name in the reverse order.
0
source

The Java Language specification defines a package naming convention that package names must include a domain name, as it provides a global namespace.

The source files must be in subfolders that match the name of the package, because the Sun Java compiler, javac , uses , greatly encourages it. In addition, many other build tools and IDEs also strongly recommend or require that the source .java files be saved in package paths.

-1
source

All Articles