154

What are the meaning of the packages org and com in Java?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Gabriel
  • 1,549
  • 2
  • 9
  • 3

4 Answers4

131

According to Sun, packages should be namespaced according to the inverse of your domain name, and then followed by whatever you see fit. Most companies or organisations have a .com, or .org domain name, and hence most packages start with com. or org.. To quote from the Sun Code Conventions:

The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.

Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.

And the examples that they give, make it pretty clear that you are meant to use the companies DNS name:

com.sun.eng

com.apple.quicktime.v2

edu.cmu.cs.bovik.cheese

You will also see edu. and net. packages out in the wild as well, although they are less common.

Community
  • 1
  • 1
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • 3
    What if the company changes domains? – Leonardo Raele Oct 02 '13 at 12:21
  • 4
    @LeonardoRaele Typically in this case then you enter a world of pain ;-) More seriously, there are two options, rename all classes, which can work if the code is internal anyway, or leave it using the old name, since that will probably still be unique anyway. – Paul Wagland Oct 02 '13 at 22:16
  • 2
    What is the best naming convention for when you're not part of an organization or don't have a website? I've seen `me.username` suggested before but I'm not sure if that's the best or the only standard. – Aaron Franke Dec 09 '16 at 01:14
  • 3
    @AaronFranke I've seen people use their GitHub username as their domain, so, for example: com.guthub.pwagland.xxx the primary purpose is to get a unique name, so that you don't have to change it, and it will never conflict with the name that someone else has chosen. – Paul Wagland Dec 24 '16 at 07:19
  • 1
    what if your company has 2 domains i.e. wordpress.com and wordpress.org – Janac Meena Mar 01 '17 at 15:31
  • 1
    @JanacMeena then just choose one of them. – Paul Wagland Mar 01 '17 at 16:30
49

You can also see package names as reversed internet domain names (which is often also true in real world, see for example the org.apache.commons which correlate with http://commons.apache.org). The com (commercial) and org (organization) are here then actually Top Level Domain names.

Package names are in general just to identify the manfacturer/vendor of the code you're facing.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 18
    And to ensure package name uniqueness. I Think it is a pretty smart Way to do it – Thorbjørn Ravn Andersen Jan 24 '10 at 00:08
  • 2
    I may be wrong, but com sounds unnecessary at this point if everyone is going to use it. I'd rather just use the company name by itself. I prefer the naming system that they have for other packaging systems like npm and composer. They use 'username/packageName' or something similar. So I would name my packages in java as 'username.packagename' – OzzyTheGiant Sep 18 '17 at 14:47
11

Usually com is used by companies when naming the packages, com being followed by the company's name. For instance you have the com.sun packages in the JVM.

The org package prefix is mostly used by non-profit organizations or for open source code, such as apache, w3c, etc.

Guillaume
  • 5,535
  • 1
  • 24
  • 30
  • The convention of using a comapany's .com URL for internal code and .org for any code made public is a good idea. That's what I summarized from this answer. – Dennis Jan 06 '13 at 15:02
  • 4
    Dennis, I don't think it would be a good idea. It means that releasing the code would change package names and would break the internal software that is using the original names. Also, there would be a namespace collision if example.com and example.org are owned by different companies and both want to release Java code. – proski Jun 11 '15 at 02:12
5

See Oracle doc for package naming

See Naming Conventions for class/interface/annotations/etc standard naming

package name and class standard syntax:
<your domain in reverse>.<project name>.<controller/dao/service/handlers etc>.<your class>

example1: (here domain:- "stackoverflow.com", project:- "Test")
com.stackoverfllow.test.handler.TestHandler

example2: (here domain:- "www.google.co.in", project:- "My Proj")
in.co.google.myproj.controller.MainController

but for reserved domains like java.*, javax.*, sun.*, etc. you should get permission from oracle community