The standard way to avoid package name conflicts in Java is to use reverse-domain convention com.[mycompany].[rest-of-the-package-name]
. This works brilliantly ... if one owns the domain [mycompany].com.
However, there are several individual developers (or students) who:
This alone introduces scope for package conflicts.
Further, assume that I own [mycompany].com. What is preventing a developer from creating a library with the same package prefix as mine and distributing it? AFAIK, there is no legal binding with regard to package names ("you MUST own the domain which you have used in you Java package"). Not to mention that this action on the developer's part might not be intentional (how many of us comb the web looking for a non-existent domain name before we name our packages?).
Even further, even if I own a domain name, it may or may not be suitable for use as a package name (my domain name could include hyphens, or it could be so long that although it is a legal package name, it could still make it impractical).
So my question is: How do I really make my Java package name unique?
You cannot absolutely guarantee that your package names will be globally unique. As you mentioned, there's nothing stopping someone else from using an identical package name, regardless of who owns whatever it is the package name is based on. There's no authoritative index of package names in use.
I see two answers to your problem:
net.sf.myproject
(or use any similar free host, such as GitHub). Such a package name is likely not to be used by others, although there is no guarantee as Matt Hurne points outIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With