3

I want to understand the packing methodology in real big projects.

Suppose we have a package com.abc.xyz, and for this, we really have a path like com/abc/xyz.

Is it possible to have multiple same package names in different directory structure like:

Directory path 1: /home/user1/project/module1/src/java/com/abc/xyz

Directory path 2:

/home/user1/project/module2/src/java/com/abc/xyz

And finally when we create jar for the whole project, do we create jar with respect to com directory?

When some application uses import com.abc.xyz, how does it know which directory path's package it is referring to?

And finally, is there any good book/resource which gives guidelines about packaging, how to divide project into modules, package names etc.

One more thing, does a project have common package base name like in above case: com.abc.xyz (e.g., org.apache.hadoop ).

Thanks, Vipin

CuriousMind
  • 8,301
  • 22
  • 65
  • 134

4 Answers4

4

Packages created in different source directories are the same package, as far as the classloader is concerned. It also doesn't matter if the class files are in the same jar or different jars. The JVM does not discriminate based on where the source code came from. (Of course if you have two jars loaded by different classloaders those are going to be treated differently.)

One case where you frequently have different source trees with the same package is when you have tests in a different directory (using the usual Maven convention where the code is under src/main/java and the tests are in src/test/java) but with the same package as the code that they exercise. These tests are able to exercise protected and package-private parts of the code under test, because they're in the same package as that code.

The path of directories inside the jar should start at the root of the package. (The topmost directory should be /, then one called com or org or whatever, etc.) Packages do form a tree-like structure, and when you put your code in a filesystem you end up having a hierarchy of packages, but the language itself doesn't recognize a concept of "subpackage" (except that packages that start with java are special and get special treatment by the classloader).

Organizing code into packages is done differently by different people. Some people like to organize their code by layer (putting all controllers in one package, all services in another package, all daos in still another package), some like to organize their code by feature.

Package-by-layer is the conventional way of organizing code, it seems to be the preferred practice in the Java community. One consequence of this is that when code implements a feature as a vertical slice at right angles to the package structure (as it may require a new controller endpoint, maybe a new service method, etc.), so closely-related bits of code for the same feature end up scattered across different directories. The Java Practices website makes an interesting case for package-by-feature:

Package By Feature Package-by-feature uses packages to reflect the feature set. It tries to place all items related to a single feature (and only that feature) into a single directory/package. This results in packages with high cohesion and high modularity, and with minimal coupling between packages. Items that work closely together are placed next to each other. They aren't spread out all over the application. It's also interesting to note that, in some cases, deleting a feature can reduce to a single operation - deleting a directory. (Deletion operations might be thought of as a good test for maximum modularity: an item has maximum modularity only if it can be deleted in a single operation.)

Here's an SO question asking about package by feature or layer.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I advice to start at least like this, ex: [com].[mydomain].[mymodule].[mysubmodule].[layer].[subcategory] where [mymodule] is the application or a library [mysubmodule] is something like client, server, android, commons (for libraries). Layer is something like service, ui, web and so on. – kalamar Apr 04 '14 at 15:34
2

Yes, you could make duplicate packages in separate directories, but I can't think of a good reason to do it. If the classes within the package have the same names you can certainly get namespace collisions. I am not sure what "module" means in this context but I'd recommend

  • com.abc.module1.xyz
  • com.abc.module2.xyz

instead. Those would be distinct packages to the classloader. You can still keep your /home/user1/project/module1/ directory structure up front, that doesn't matter.

Erica Kane
  • 3,137
  • 26
  • 36
1

From 2 modules you will have two seperate jar files: module1.jar and module2.jar. Both will be loaded into ClassLoader when application starts.

When some application uses import com.abc.xyz, how does it know which directory path's package it is referring to?

Classloader will handle that. http://www.javaworld.com/article/2077260/learn-java/the-basics-of-java-class-loaders.html

If you trying to develop multi module application i recommend you to check Maven tool:

http://maven.apache.org/

Why maven? What are the benefits?

For guidance for package organization you can just google 'java packages' phrase. http://www.tutorialspoint.com/java/java_packages.htm

Community
  • 1
  • 1
smajlo
  • 972
  • 10
  • 21
1

https://www.facebook.com/Niranthara-Jaya-JavaSocial-Media-Apps-Software-Project-Management-244119296136021/

This page is for people who wish to know how to work with real world Java projects. Send a message to this page and check out the articles.

Matt Jerry
  • 501
  • 1
  • 3
  • 12