开发者

Java: Point of subdirectories

First of all: I'm not entirely familiar with Java, and the few things I know I have learned while playing with Java.

However, there is something I have noticed开发者_高级运维 in pretty much any Opensource Java project - the use of alot of subdirectories for the sources, which usually look like so:

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

Now, why so many subdirectories? what's the deal with the domainname?


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

com.somedomainname.projectname.sourcefile

where com.somedomainname.projectname is the package.

Conventionally, source file organization mirrors the package layout. The normal Java compiler doesn't actually enforce directory structure (although some IDEs such as Eclipse will complain if you put things in the "wrong" directories) but it does force public classes to be in a file with the same name. Non-public classes can go in any file, but conventionally the filename matches the class name there, too. It makes it very easy to navigate to any class without any prior knowledge.

The Java language specification doesn't say that a compiler must enforce the convention for public classes; it explicitly says that it can though. See section 7.2 of the JLS for more details.


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

One reason to use this structure is that is the standard used by Maven.

Maven is a build tool that helps to manage the dependencies of a project. Maven is designed for convention over configuration, so you will often see this directory structure to make it work with Maven.

Maven specifies that the directory structure start with /src/main/java for Java files, and the rest is based on the naming convention for namespaces.

The use of the domain name in the path is to prevent class collisions. If 2 different libraries both supply a class with the same name, the domain name namespace allows them to both be used.


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

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


subdirectories as an organizational tool so that you don't just have one directory with tons of java files. The reason you often see a domain name is that conventionally people derive java package names from their domain names in order to prevent collisions with other developers. So although we both might have a util.Stringutil class, if I name mine com.mydomain.util.Stringutil and yours is com.yourdomain.util.Stringutil, we can have a project containing both classes without a collision.


There is an interesting read on java packages and directories in the newer O'Reilly book Java: The Good Parts (starting at the bottom of page 46).

...the required interaction between the package system and the filesystem is both regrettable and a pain...


This is meant as a standard to define unique locations for java source code. It is convention to follow this package structure, which is why you see it everywhere. It's not required to do it that way - you can name your packages whatever you want. It is very commonplace to follow this convention, however.

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 translates into a subdirectory. So the same three classes shown above would be located in the corresponding directories off the main classpath.

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

When compiling by hand, be sure that the main classpath directory is the current directory or is within the classpath in order that the source code files can be found.

As for the src/main/java part of it, it seems this comes from Maven. I've never used that software. I don't understand why they would need so many, since my projects (I use Eclipse) just have a src folder there instead.


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

  1. src/main/java
    this is the directory that needs to be passed to the javac compiler stating where the source code for compilation can be found.
    1.1 src/test/java
    this is where the unit test classes should be kept.
    1.2 src/main/resources and src/test/resources
    these are the corresponding directories where resources such as properties files should be kept. 1.3 Separate output directories.
    main and *test * classes and resources should be compiled to their own separate output directories. Maven uses target/classes and target/test-classes. When you jar your compiled class files for distribution, you don't want to include test classes and test resource files.
  2. com/somedomainname/projectname
    this directory structure corresponds to the package declaration in the classes found in projectname i.e. package com.somedomainname.projectname
  3. SourceFile.java corresponds to the class name that it defines, and it should by convention start with an uppercase character see http://www.oracle.com/technetwork/java/codeconvtoc-136057.html
  4. Also in the link above you will find out that the default package naming convention uses the domain name in reverse.


The Java Language Specification defines a package naming convention that says that package names should include a domain name, as it provides a globally-rooted namespace.

The source files need to be in subfolders that match the package name, because the Sun Java compiler, javac, enforces strongly encourages it. Additionally, many other build tools and IDEs also either strongly encourage or require that the source .java files are stored in paths that match the package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜