49

Possible Duplicate:
Java packages com and org

I am a java developer. Nowadays I am learning struts and when reading a tutorial a curiosity intruded in my mind regarding

package com.something.something;

I know it is a very simple package declaration but what about

package **com**.something.something;

This package name fragment often comes in many commercial distributions. Now I want to know what does it mean? Please clarify it.

Thanks and sorry if I couldn't clarify it...

Shubham
  • 2,847
  • 4
  • 24
  • 37
Nishant Kumar
  • 970
  • 1
  • 8
  • 11
  • If you are asking we put com before the package name then the answer is just convention no other reason. Some people say it's reverse of URL or XML namespace but it is just convention nothing else. – Shahzeb Sep 14 '11 at 05:40
  • Actually, you should follow what others are saying below... – Nishant Kumar Sep 14 '11 at 06:22
  • if you have written a single class ever you would know this not domain name where as it can be so could it be your surname.lastname . Sun-Oracle and documentation can say what ever they feel approperiate but when I spend time day in day out creating classess I know they are almost never domain names.try out org.apache.commons or commons.apache.org and tell me how does their welcome page look like. – Shahzeb Sep 14 '11 at 06:31
  • Yes, may be you are right, it depends upon project's nature. For commercial projects we can create package using domain_TLD.companyname.xyz or for home project we can go as we are comfortable... – Nishant Kumar Sep 15 '11 at 07:02
  • Based on all the feedback on this topic, it appears that package declaration naming convention play absolutely no role other than enforcing an unique identifier. Further more if everyone starts package declarations with a domain of "com" or "org", does this not negate the point of it all? – Gerrit Brink Sep 05 '13 at 13:41
  • it just namespace but if you use that package publically there are some rules for it, ex.you can not use package name starts with java. or javax. without permission, it name according your domain name for ignore duplicate namespace with others, for ex. domain is **www.abcdef.co.in** then package name should **in.co.abcdef.my_proj_name.\*** then it will never mismatch with others, for more info please read [Oracle doc](http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html) – Dharmendrasinh Chudasama Sep 27 '17 at 05:22

6 Answers6

48

It's just a namespace definition to avoid collision of class names. The com.domain.package.Class is an established Java convention wherein the namespace is qualified with the company domain in reverse.

Sahil Muthoo
  • 12,033
  • 2
  • 29
  • 38
  • 2
    Related docs: http://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html – Chip McCormick Feb 13 '15 at 21:53
  • 8
    What should I do if I don't have my own domain? – Stephen Holt Jan 31 '17 at 20:40
  • 3
    @StephenHolt If your code is open-source and hosted in a public repository, there is a popular trend of using the domain of the source control repository itself. For example, many popular projects are still using `net.sourceforge` or `net.sf` followed by their project names. These days, both GiHub and GitLab provide static web hosting for your projects at `project.gihub.io` and `project.gitlab.io` so that can be another option to explore. – Mrinal Nov 27 '17 at 01:14
43

Wikipedia, of all places, actually discusses this.

The idea is to make sure all package names are unique world-wide, by having authors use a variant of a DNS name they own to name the package. For example, the owners of the domain name joda.org created a number of packages whose names begin with org.joda, for example:

  • org.joda.time
  • org.joda.time.base
  • org.joda.time.chrono
  • org.joda.time.convert
  • org.joda.time.field
  • org.joda.time.format
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
10

It's the domain name spelt out in reverse.

For example, one of my domains is hedgee.com. So, I use com.hedgee as the base name of all my packages.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
4

From the Wikipedia article on Java package naming:

In general, a package name begins with the top level domain name of the organization and then the organization's domain and then any subdomains, listed in reverse order. The organization can then choose a specific name for its package. Package names should be all lowercase characters whenever possible.
For example, if an organization in Canada called MySoft creates a package to deal with fractions, naming the package ca.mysoft.fractions distinguishes the fractions package from another similar package created by another company. If a US company named MySoft also creates a fractions package, but names it us.mysoft.fractions, then the classes in these two packages are defined in a unique and separate namespace.
jimbo
  • 11,004
  • 6
  • 29
  • 46
3
  • com => domain
  • something => company name
  • something => Main package name

For example: com.paresh.mainpackage

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com. This information i have found at http://download.oracle.com/javase/tutorial/java/package/namingpkgs.html

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • 3
    This answer is missing the very important relation to actual, registered domain names. That is what *ensures* that your package name is unique. – Joachim Sauer Sep 14 '11 at 05:41
3

This is what Sun-Oracle documentation says:

Package names are written in all lower case to avoid conflict with the names of classes or interfaces.

Companies use their reversed Internet domain name to begin their package names—for example, com.example.mypackage for a package named mypackage created by a programmer at example.com.

Andrey Atapin
  • 7,745
  • 3
  • 28
  • 34